Original Post
I've got a program that is supposed to require one object of class mySingletonObject, and this object needs to be available from anywhere in the program. The object is created in the main function, and it does work to stop another from being made within main. However, mySingleton object contains members made from other classes, which inturn contain members made from other classes, which contain functions that need to get information from the mySingletonObject in which they are nested. I wanted to avoid having to pass the mySingleton object into these functions, simply because the way things are interconnected I would endlessly be passing this reference between multiple classes/functions to get it to the function that actualy needs it. It seemed far easier to create the object as a singleton (sort of a pseudo global), since I only wanted one anyway. The problem is that an object nested within the mySingleton object seems to be able to create more mySingleton objects. Even worse, because one of these objects is created in the constructor of an object that is created in the mySingleton object , and then in turn looks for the mySingleton object (calling the funtion that should either return the reference or create a new object), the program ends up in an endless reiteration of constructors. The mySingleton object creates an object which doesn't recongnize that a mySingleton object already exists, so it creates another mySingleton object, which creates an object which doesn't recongnize that a mySingleton object already exists... This is basic code: //mySingleton.h #ifndef CLASS_MYSINGLETON_H #define CLASS_MYSINGLETON_H #include "Nest.h" //A Singleton class, this should be able to only be instanced once class mySingleton { public: static mySingleton* Instance(); static void Destroy(); protected: mySingleton(); mySingleton(const mySingleton&); mySingleton& operator= (const mySingleton&); ~mySingleton(); private: static mySingleton* _instance; Nest* nest; int size; }; #endif //CLASS_MYSINGLETON_H //mySingleton.cpp #include "mySingleton.h" mySingleton* mySingleton::_instance = 0; mySingleton::mySingleton() { this->nest = new Nest; this->size = rand() % 10; } mySingleton::~mySingleton() { delete this->nest; } mySingleton* mySingleton::Instance() { if(_instance == 0) { _instance = new mySingleton; } return _instance; } void mySingleton::Destroy() { delete _instance; _instance = 0; } //Nest.h #ifndef CLASS_NEST_H #define CLASS_NEST_H #include "Egg.h" class Nest { public: Nest(); ~Nest(); private: Egg* egg; }; #endif //CLASS_NEST_H //Nest.cpp #include "Nest.h" Nest::Nest() { this->egg = new Egg; } . . . //Egg.h #ifndef CLASS_EGG_H #define CLASS_EGG_H class Egg { public: Egg(); ~Egg(); private: int type; }; #endif //CLASS_EGG_H //Egg.cpp #include "mySingleton.h" Egg::Egg() { this->type = mySingleton::Instance()->size; } . . . //main.cpp #include "mySingleton.h" int main(int argc, char *argv[]) { mySingleton* tree = new mySingleton; } As far as I know, mySingleton::Instance() should not be creating a new object when one already exists. Can anyone tell me why it is? Is there a better way to accomplish what I want? Thanx