LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2005, 06:45 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
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
 
Old 11-07-2005, 06:51 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
What exactly is your function definition?

Is it static MyClass* MyClass::getInstance(); or is it MyClass MyClass::getInstance();?
 
Old 11-07-2005, 07:19 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
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)
 
Old 11-07-2005, 07:21 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 11-13-2005, 02:30 PM   #5
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
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.
 
Old 11-13-2005, 04:48 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 11-13-2005 at 04:49 PM.
 
Old 11-13-2005, 07:23 PM   #7
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
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.
 
Old 11-13-2005, 07:45 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM
Pattern Recognition trbdk3 Linux - Software 1 01-27-2005 01:13 PM
pattern filter help required m3rc_2004 Linux - General 0 08-19-2004 10:36 AM
pattern matchin with perl gene_gEnie Programming 6 09-29-2001 11:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration