LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   singleton pattern in C++? (https://www.linuxquestions.org/questions/programming-9/singleton-pattern-in-c-380732/)

Thinking 11-07-2005 06:45 AM

singleton pattern in C++?
 
hiho@ll

i had the same problem like this poster: http://coding.derkeiler.com/Archive/...4-01/1176.html

and i did it like posted there

so, i have a solution to my problem
but i want to know why it is such a problem to have a pointer for a singleton pattern in C++?
i did the same stuff in Visual C++ and it worked great
but with gcc i get "undefined reference" for my instance pointer

it's exactly like the post above

but why?

now i'm using the reference operator and a "local" static instance of my class and it works

but why is it such a problem to do

MyClass::getInstance(){
if(MyClass::instance==NULL)
MyClass::instance=new MyClass();
return instance;
}

instead of

MyClass::getInstance(){
static MyClass theonlyone;
return theonlyone;
}

thx@ll

jtshaw 11-07-2005 06:51 AM

What exactly is your function definition?

Is it static MyClass* MyClass::getInstance(); or is it MyClass MyClass::getInstance();?

Thinking 11-07-2005 07:19 AM

the getinstance is static

i would post a code
but i alredy changed it so i don't have it in this form

maybe i can reproduce it in the next few hours (cause i don't have much time)

jtshaw 11-07-2005 07:21 AM

If you can't at least post the actual function prototype (Because both of the ones in your original post won't work) then I can only make stabs in the dark...

It really would be helpful to have the entire class definition as you created it.

FLLinux 11-13-2005 02:30 PM

I have done a singleton for C++ in gcc like this

In the header you do something like this
class MyClass
{
public:
static MyClass* getInstance();

private:

MyClass();
~MyClass();

static MyClass* _pInstance;
};

Then in the cc file do this
MyClass* MyClass::_pInstance = NULL;

MyClass::MyClass(){}
MyClass::~MyClass(){}

MyClass* MyClass::getInstance()
{
if(_pInstance == NULL)
{
_pInstance = new MyClass();
}

return _pInstance;
}

It is also a good idea to but a mutex around the creation of the object if more the one thread is going to be getting this object. This singleton should work with either gcc or the Visual C++ compiler.

dmail 11-13-2005 04:48 PM

Im not sure making the destructor private is correct, i always put it in public, plus your comment about the mutex is not really correct. singleton classes are not thread safe and it takes more than introducing a mutex to try and make it safe, i did read a paper on this but at the moment i can't seem to find it. ;( basically the compiler is the problem and does things which you wouldnt think, not really a good explanation but hey ho.

FLLinux 11-13-2005 07:23 PM

If you don't make the destructor private then anyone that gets the pointer to the object can delete it.

Would like to see paper on the thread safe issue.

dmail 11-13-2005 07:45 PM

here you are http://www.aristeia.com/Papers/DDJ_J...04_revised.pdf it made me laugh when i first read it, it seems the best way is to create a wraper for the class which has a mutex in it.


All times are GMT -5. The time now is 03:29 PM.