• Advertisement
Sign in to follow this  

c++ Struct Question

This topic is 3851 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

Is there some way i can make sure only one instance of a struct is ever created?

Share this post


Link to post
Share on other sites
Advertisement
Yes, it's possible.

struct TheStruct
{
static TheStruct & Instance()
{
static TheStruct instance;
return instance;
}

private:
TheStruct();
TheStruct(const TheStruct &);
};


There's a very high probability that doing this is useless (i.e. you don't need the protection), and a high probability that your design will cause you great pain in a few weeks or months.

Share this post


Link to post
Share on other sites
A singleton or similarly a monostate.

*A monostate allows multiple instances but all instances share the same state.

[edit]

You could probably avoid both however.

[Edited by - fpsgamer on August 5, 2007 10:19:27 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement