Single instance issue

S

Sarath

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
 
I

Ian Collins

Sarath said:
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?
private:
CSingleton(){}
~CSingleton(){}
}
It wouldn't compile with any compiler without the missing semicolon.
The above code failed to compile in Visual C++ 6.0

Never trust that compiler, it's old and not very standards compliant.
 
S

Sarath

How can you call this method and how would you change its definition if
you did want to call it?


It wouldn't compile with any compiler without the missing semicolon.


Never trust that compiler, it's old and not very standards compliant.

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
 
S

Salt_Peter

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...
 
S

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
 
J

James Kanze

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.)
So what about the compiler generated copy constructor?

Good point.
int main()
{
CSingle instance;
CSingle copy(instance);
}
and assignment? etc...

Assignment would require two instances, or... Making it private
certainly doesn't hurt, however, and IMHO makes the intent
clearer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top