Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnnyBravo

Need "simple" singleton class example

This topic is 5330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 implementation


public:
// 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 ]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!