Need "simple" singleton class example

Started by
32 comments, last by johnnyBravo 20 years, 5 months ago
I want to start using singleton classes, but wherever i look the examples include other stuff that i cannot differentiate from the actual singleton class code. So if someone could provide an example or modify this code here to be a singleton class, would be really great.

class Apple {
public:
   Apple()
   {
   }

   ~Apple()
   {
   }
   
   void myMethod()
   {
   }

private:
};

thanks,
Advertisement
class Apple{private:   // Prevent creation of Apple instances from client code   Apple() { /* constructor code */ }      // Disable copying (and pass-by-value)   Apple(const Apple&);            // no implementation    Apple& operator=(const Apple&); // no implementation  // The instance will have to be destroyed when the  // program finally terminates.  ~Apple() { /* destructor code */ }public:  // This is where you get your singleton instance from  static Apple* GetInstance()  {     static Apple instance;     return &instance  }};Apple* ptr = Apple::GetInstance();


edit: Curse you, SiCrane !

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on November 9, 2003 6:09:20 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ack, no. If you use a Meyer''s Singleton to manage lifetime and you return a pointer from your instance function, make the destructor non-public so that the client isn''t tempted to delete the returned pointer.
quote:Original post by SiCrane
Ack, no. If you use a Meyer''s Singleton to manage lifetime and you return a pointer from your instance function, make the destructor non-public so that the client isn''t tempted to delete the returned pointer.


My original code returned a reference, but then I thought about the hassle of having him actually use reference ''variables''. So, yeah. Once again, *sigh*.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by SiCrane
Ack, no. If you use a Meyer's Singleton to manage lifetime and you return a pointer from your instance function, make the destructor non-public so that the client isn't tempted to delete the returned pointer.



i get this error message if i do that
quote:
error C2248: 'g::~g' : cannot access private member declared in class 'g'
see declaration of 'g::~g'


so i put the destructor in the public part and it did, is there anything else i should do instead?

edit:

just one more thing, how would i use the class,
i got this error when i tried doing it like this
Apple myApple;

quote:
error C2248: 'D3D::D3D' : cannot access private member declared in class 'D3D'
see declaration of 'D3D::D3D'


[edited by - johnnyBravo on November 9, 2003 6:44:49 AM]
quote:Original post by johnnyBravo
just one more thing, how would i use the class,
i got this error when i tried doing it like this
Apple myApple;


That''s exactly the point behind using a Singleton, to prevent you from ever doing that. Use the value returned by Apple::GetInstance() instead.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
What compiler are you using? All the ones I tried will let the destructor be private.

With the version of the Singleton that Fruny described, you always access the Apple through Apple::GetInstance(). You don''t need to create it or allocate it. The Apple will construct itself the first time you call Apple::GetInstance().
Thankyou both!

This is exactly what i needed for my program.

I have visual c++ 6.
Its kinda annoying that i cant have the destructor in the private, as you said, to stop people from calling it.
Well, you could always do

class Apple{private:   // Prevent creation of Apple instances from client code   Apple() { /* constructor code */ }      // Disable copying (and pass-by-value)   Apple(const Apple&);            // no implementation    Apple& operator=(const Apple&); // no implementationpublic:  // The instance will have to be destroyed when the  // program finally terminates.  ~Apple() { /* destructor code */ }  // This is where you get your singleton instance from  static Apple& GetInstance()  {     static Apple instance;     return instance;  }};Apple& ref = Apple::GetInstance();


The usual caveats about references apply.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
hmm isnt that just the same as the other one? cept with the destructor in public which i had to do anyway?



one more question, is there a singleton way that i can call it like

Apple myApple;
?

Its for other things...

This topic is closed to new replies.

Advertisement