Connecting Tech Pros Worldwide Help | Site Map

Single instance issue

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 21st, 2007, 05:45 AM
Sarath
Guest
 
Posts: n/a
Default Single instance issue

I've to write a single instance class. there are different methods to
control the single instance of a program. I've tried the following
method

class CSingleton
{
public:
CSingleton& GetInstance(){ static CSingleton s; return s; }

private:
CSingleton(){}
~CSingleton(){}
}


The above code failed to compile in Visual C++ 6.0 but compiled in
Visual C++ 7.1 and Visual C++ Express 2008. CRT (in microsoft concept)
calling the destructor of the class. So that Visual C++ 6.0
compilation error (Failed to access destructor) is correct according
to the concept.

I also tried in Dev C++. It was successful but didn't call the dtor of
the class. Which implementation is correct according to the standard.

Sorry if off-topic to the forum

  #2  
Old December 21st, 2007, 06:55 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Single instance issue

Sarath wrote:
Quote:
I've to write a single instance class. there are different methods to
control the single instance of a program. I've tried the following
method
>
class CSingleton
{
public:
CSingleton& GetInstance(){ static CSingleton s; return s; }
>
How can you call this method and how would you change its definition if
you did want to call it?
Quote:
private:
CSingleton(){}
~CSingleton(){}
}
>
It wouldn't compile with any compiler without the missing semicolon.
Quote:
The above code failed to compile in Visual C++ 6.0
Never trust that compiler, it's old and not very standards compliant.

--
Ian Collins.
  #3  
Old December 21st, 2007, 07:15 AM
Sarath
Guest
 
Posts: n/a
Default Re: Single instance issue

On Dec 21, 4:49*pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Sarath wrote:
Quote:
I've to write a single instance class. there are different methods to
control the single instance of a program. I've tried the following
method
>
Quote:
class CSingleton
{
public:
* * CSingleton& GetInstance(){ static CSingleton s; return s; }
>
How can you call this method and how would you change its definition if
you did want to call it?
>
Quote:
private:
* * CSingleton(){}
* * ~CSingleton(){}
}
>
It wouldn't compile with any compiler without the missing semicolon.
>
Quote:
The above code failed to compile in Visual C++ 6.0
>
Never trust that compiler, it's old and not very standards compliant.
>
--
Ian Collins.
Dear All,
I'm extremely sorry to paste wrong code. Please refer this one.

class CSingle
{
public:
static CSingle& GetInstance(){ static CSingle s; return s; }

private:
CSingle() { cout<<"ctor"; }
~CSingle() { cout<<"dtor"; }
};

Sorry for the incovenience.Please refer this code for my question

Regards,
Sarath
  #4  
Old December 21st, 2007, 07:45 AM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Single instance issue

On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
Quote:
On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.comwrote:
>
>
>
Quote:
Sarath wrote:
Quote:
I've to write a single instance class. there are different methods to
control the single instance of a program. I've tried the following
method
>
Quote:
Quote:
class CSingleton
{
public:
CSingleton& GetInstance(){ static CSingleton s; return s; }
>
Quote:
How can you call this method and how would you change its definition if
you did want to call it?
>
Quote:
Quote:
private:
CSingleton(){}
~CSingleton(){}
}
>
Quote:
It wouldn't compile with any compiler without the missing semicolon.
>
Quote:
Quote:
The above code failed to compile in Visual C++ 6.0
>
Quote:
Never trust that compiler, it's old and not very standards compliant.
>
Quote:
--
Ian Collins.
>
Dear All,
I'm extremely sorry to paste wrong code. Please refer this one.
>
class CSingle
{
public:
static CSingle& GetInstance(){ static CSingle s; return s; }
>
private:
CSingle() { cout<<"ctor"; }
~CSingle() { cout<<"dtor"; }
>
};
>
Sorry for the incovenience.Please refer this code for my question
>
Regards,
Sarath
declare your destructor public. No reason to hide it.
So what about the compiler generated copy constructor?

int main()
{
CSingle instance;
CSingle copy(instance);
}

and assignment? etc...



  #5  
Old December 21st, 2007, 11:25 AM
Sarath
Guest
 
Posts: n/a
Default Re: Single instance issue

On Dec 21, 5:41 pm, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
>
>
>
Quote:
On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.comwrote:
>
Quote:
Quote:
Sarath wrote:
I've to write a single instance class. there are different methods to
control the single instance of a program. I've tried the following
method
>
Quote:
Quote:
class CSingleton
{
public:
CSingleton& GetInstance(){ static CSingleton s; return s; }
>
Quote:
Quote:
How can you call this method and how would you change its definition if
you did want to call it?
>
Quote:
Quote:
private:
CSingleton(){}
~CSingleton(){}
}
>
Quote:
Quote:
It wouldn't compile with any compiler without the missing semicolon.
>
Quote:
Quote:
The above code failed to compile in Visual C++ 6.0
>
Quote:
Quote:
Never trust that compiler, it's old and not very standards compliant.
>
Quote:
Quote:
--
Ian Collins.
>
Quote:
Dear All,
I'm extremely sorry to paste wrong code. Please refer this one.
>
Quote:
class CSingle
{
public:
static CSingle& GetInstance(){ static CSingle s; return s; }
>
Quote:
private:
CSingle() { cout<<"ctor"; }
~CSingle() { cout<<"dtor"; }
>
Quote:
};
>
Quote:
Sorry for the incovenience.Please refer this code for my question
>
Quote:
Regards,
Sarath
>
declare your destructor public. No reason to hide it.
So what about the compiler generated copy constructor?
>
int main()
{
CSingle instance;
CSingle copy(instance);
>
}
>
and assignment? etc...
It will work if I make the dtor public. But I just want to know about
the destruction of static objects in the above scenario. As All
compilers behaves in different manner.

Regards,
Sarath
  #6  
Old December 21st, 2007, 02:45 PM
James Kanze
Guest
 
Posts: n/a
Default Re: Single instance issue

On Dec 21, 9:41 am, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
Quote:
Quote:
I'm extremely sorry to paste wrong code. Please refer this one.
Quote:
Quote:
class CSingle
{
public:
static CSingle& GetInstance(){ static CSingle s; return s; }
Quote:
Quote:
private:
CSingle() { cout<<"ctor"; }
~CSingle() { cout<<"dtor"; }
};
Quote:
Quote:
Sorry for the incovenience.Please refer this code for my question
Quote:
declare your destructor public. No reason to hide it.
There's also no reason to make it public, since the code should
work if the destructor is private as well. (A long time ago,
this was a frequent error, since the pre-standard specification
wasn't too clear as to where access of the destructor should be
checked. But any modern compiler should get it right.)
Quote:
So what about the compiler generated copy constructor?
Good point.
Quote:
int main()
{
CSingle instance;
CSingle copy(instance);
}
Quote:
and assignment? etc...
Assignment would require two instances, or... Making it private
certainly doesn't hurt, however, and IMHO makes the intent
clearer.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,989 network members.