Single instance issue

Discussion in 'C++' started by Sarath, Dec 21, 2007.

  1. Sarath

    Sarath Guest

    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
    Sarath, Dec 21, 2007
    #1
    1. Advertising

  2. Sarath

    Ian Collins Guest

    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.
    Ian Collins, Dec 21, 2007
    #2
    1. Advertising

  3. Sarath

    Sarath Guest

    On Dec 21, 4:49 pm, Ian Collins <> 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
    Sarath, Dec 21, 2007
    #3
  4. Sarath

    Salt_Peter Guest

    On Dec 21, 3:11 am, Sarath <> wrote:
    > On Dec 21, 4:49 pm, Ian Collins <> 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...
    Salt_Peter, Dec 21, 2007
    #4
  5. Sarath

    Sarath Guest

    On Dec 21, 5:41 pm, Salt_Peter <> wrote:
    > On Dec 21, 3:11 am, Sarath <> wrote:
    >
    >
    >
    > > On Dec 21, 4:49 pm, Ian Collins <> 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
    Sarath, Dec 21, 2007
    #5
  6. Sarath

    James Kanze Guest

    On Dec 21, 9:41 am, Salt_Peter <> wrote:
    > On Dec 21, 3:11 am, Sarath <> 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:
    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
    James Kanze, Dec 21, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    1
    Views:
    603
    S. Justin Gengo
    Dec 12, 2005
  2. Chris
    Replies:
    1
    Views:
    13,634
    Oisin
    Mar 24, 2006
  3. chris brat
    Replies:
    1
    Views:
    631
    chris brat
    May 10, 2006
  4. Eric Layman
    Replies:
    3
    Views:
    625
    Rad [Visual C# MVP]
    Apr 14, 2007
  5. goodfella

    single producer, single consumer

    goodfella, Oct 6, 2009, in forum: C++
    Replies:
    41
    Views:
    1,927
    James Kanze
    Oct 12, 2009
Loading...

Share This Page