473,408 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
Dec 21 '07 #1
5 1678
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

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.

--
Ian Collins.
Dec 21 '07 #2
On Dec 21, 4:49*pm, Ian Collins <ian-n...@hotmail.comwrote:
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
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.

--
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
Dec 21 '07 #3
On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.comwrote:
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
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.
--
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...

Dec 21 '07 #4
On Dec 21, 5:41 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.comwrote:
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
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.
--
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...
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
Dec 21 '07 #5
On Dec 21, 9:41 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 21, 3:11 am, Sarath <CSar...@gmail.comwrote:
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
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.

--
James Kanze (GABI Software) email:ja*********@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
Dec 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
9
by: MrSpock | last post by:
1. Create a new Windows Application project. 2. Open the project properties and check "Make single instance application". 3. Build. 4. Go to the release folder and run the application. 5. Try to...
13
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
4
by: freefighter | last post by:
Is there such a way to ensure that at a time just one variable (pointer to ref type) is used to maintain the object. This is needed for a multi-threded application where shared data should be used...
13
by: Samir Chouaieb | last post by:
Hello, I am trying to find a solution to a login mechanism for different domains on different servers with PHP5. I have one main domain with the user data and several other domains that need...
3
by: sklett | last post by:
I suspect the answer might be in one of the words of my subject, but here goes anyway. I'm working on a system that will execute a very long (300+) chain of task objects. Here is some quick...
5
by: velu5 | last post by:
Hello , Is it possible/recommended to do SQL server instance backups in Single user mode ? Thanks in advance, atv
7
by: =?Utf-8?B?TWF0dA==?= | last post by:
Hi I have an app that runs without a main form, just a notification icon, when the user clicks the icon the form is shown, and when the form is minimized it's hidden. This all works great,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.