By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,118 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,118 IT Pros & Developers. It's quick & easy.

singleton in gcc 3.3

P: n/a
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?

Jul 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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.

Jul 25 '05 #2

P: n/a
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

Jul 25 '05 #3

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

--Steve

Jul 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.