#How do I approach creating a class with only one instance that's not accessible to everyone?

7 messages · Page 1 of 1 (latest)

molten cliffBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hot hornet
#

make constructor private and have a static sharedInstance function that instantiates a static instance of your class

#

class A {
A() = whatever
public:
static A* shared() {
static A instance;
return &instance;
}
};

#

this will lazy-load the instance which is quite useful for ensuring instantiation order is consistent if you’re using this singleton in an entrypoint

tranquil stirrup
#

since its public though, cant anyone access that instance now?

#

I saw someone do something similar, but instead of a static instance they friended the class that is supposed to be the one that can instantiate it

molten cliffBOT
#

@tranquil stirrup

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.