singleton in gcc 3.3

K

ken

template<class T>
class cSingleton : public T
{
private:
cSingleton() {};

public:
static T* GetInstance()
{
static cSingleton instance;
return &instance;
}
};

I wrote this code in gcc 3.3 with xcode.

But, 'static cSingleton instance;' doesn't work well.
I think, it's bug of gcc 3.3

Is there any other way to make singleton in gcc 3.3?
 
A

Alipha

But, 'static cSingleton instance;' doesn't work well.
I think, it's bug of gcc 3.3

"Doesn't work well" how? Error message? Please post a complete,
compilable program that demonstrates the problem and error messages, if
applicable.
template<class T>
class cSingleton : public T
{
private:
cSingleton() {};

public:
static T* GetInstance()
{
static cSingleton instance;
return &instance;
}
};

Note that the design of this singleton class is actually flawed. The
problem is that the class is templated. This means that your
GetInstance member function has internal linkage and the GetInstance
member function is being duplicated, along with the instance variable,
in each object file in your program. That is, if both a.cpp and b.cpp
use this singleton class, then a.cpp and b.cpp will be accessing
separate instance objects.
 
K

ken

Thank you, You're right,
No Error, It's applicable.

As you know, Whenever I call 'GetInstance()', instance is created.
For example, there is a class, cClass

class cClass* p1 = cSingleton <class cClass>::GetInstance();
class cClass* p2 = cSingleton <class cClass>::GetInstance();

p1 is not same with p2.

But, This code worked well with vc++.

How can I fix my code to work well?

regards,
ken
 
M

mrstephengross

Read chapter 6 of "Modern C++ Design" (Alexandrescu); it's a very
thorough explanation.

--Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top