Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2005, 04:35 PM
ken
Guest
 
Posts: n/a
Default singleton in gcc 3.3

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?

  #2  
Old July 25th, 2005, 05:05 PM
Alipha
Guest
 
Posts: n/a
Default Re: singleton in gcc 3.3

> But, 'static cSingleton instance;' doesn't work well.[color=blue]
> I think, it's bug of gcc 3.3[/color]

"Doesn't work well" how? Error message? Please post a complete,
compilable program that demonstrates the problem and error messages, if
applicable.
[color=blue]
> template<class T>
> class cSingleton : public T
> {
> private:
> cSingleton() {};
>
> public:
> static T* GetInstance()
> {
> static cSingleton instance;
> return &instance;
> }
> };[/color]

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.

  #3  
Old July 25th, 2005, 06:05 PM
ken
Guest
 
Posts: n/a
Default Re: singleton in gcc 3.3

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

  #4  
Old July 25th, 2005, 06:25 PM
mrstephengross
Guest
 
Posts: n/a
Default Re: singleton in gcc 3.3

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

--Steve

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles