Single instance issue



 DEVELOP > c-Plus-Plus > Single instance issue

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sarath"
Date: 21 Dec 2007 12:41:49 AM
Object: 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
.

User: "Ian Collins"

Title: Re: Single instance issue 21 Dec 2007 01:49:07 AM
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.
.
User: "Sarath"

Title: Re: Single instance issue 21 Dec 2007 02:11:46 AM
On Dec 21, 4:49=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:

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:
=A0 =A0 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:
=A0 =A0 CSingleton(){}
=A0 =A0 ~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
.
User: "Salt_Peter"

Title: Re: Single instance issue 21 Dec 2007 02:41:53 AM
On Dec 21, 3:11 am, Sarath <CSar...@gmail.com> wrote:

On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.com> wrote:



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...
.
User: "Sarath"

Title: Re: Single instance issue 21 Dec 2007 06:19:56 AM
On Dec 21, 5:41 pm, Salt_Peter <pj_h...@yahoo.com> wrote:

On Dec 21, 3:11 am, Sarath <CSar...@gmail.com> wrote:



On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.com> wrote:


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
.

User: "James Kanze"

Title: Re: Single instance issue 21 Dec 2007 09:44:51 AM
On Dec 21, 9:41 am, Salt_Peter <pj_h...@yahoo.com> wrote:

On Dec 21, 3:11 am, Sarath <CSar...@gmail.com> wrote:

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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER