   C++ and Class Organization
  I am not so good at C++ and at the moment I am doingsomething that feels wrong, it is like follows:class thread {public:  // Thread stuff   .   int ThreadX();   int ThreadY();  //Other stuffprivate:  // thread stuff  ClassA *A;   ClassB *B;  ClassC *C; // Other stuff}int thread::ThreadX(){ A = new ClassA; B = new ClassB; A->memberfunction1(B); B->memberfunction2(A);// Other stuff}int thread::ThreadY(){ C = new ClassC; // Wait a bit if B and A exists..go on C->memberfunction(B,A)}The thing that feels wrong is that the memberfunctionsneed access to data in other classes so I store pointers A B C andpass them along.When I add more classes here and there suddenly I am passing pointersaround like crazy..Making them global would solve things though..but people say global is badis there any other way? And why would global be so bad?   
  Instead of using global variables use "Singletons"Global variables aren't necessary bad, just messy in my opinionBut here is an article explaining why globals are badwith some alternatives to themhttp://c2.com/cgi/wiki?GlobalVariablesAreBad   ;Quote:Instead of using global variables use "Singletons"Global variables aren't necessary bad, just messy in my opinionIn what way would using a singleton be better than using a global? (Not saying it wouldn't - just wondering what your reasoning is.)   ;  Passing pointers is the right way to go.Globals are bad. Singletons are just globals which are restricted to a single instance (so they're also bad in the exact same ways that globals are).Globals are ok if you're just hacking together code and you never want to look at it again... but for anything that you want to maintain or reuse, globals make it a lot harder to reason about data ownership, access, lifetimes, synchronisation, modularity, etc... The also form an implicit interface that is used by your other classes, so if you want to reuse those classes in another app, it's forced to create the same globals for things to work.   ;Quote:Original post by projectghostInstead of using global variables use "Singletons"Global variables aren't necessary bad, just messy in my opinionBut here is an article explaining why globals are badwith some alternatives to themhttp://c2.com/cgi/wiki?GlobalVariablesAreBadSingletons are a whole separate can of worms, and aren't much better than globals.In general, passing pointers around isn't a bad idea. If it becomes tedious, your design might need some refactoring to limit dependency. But if object A really needs to communicate with objects B through K, then yes, passing a pointer to each of these objects (or a struct filled with pointers) is generally better than making global instances.   ;Quote:Original post by jykQuote:Instead of using global variables use "Singletons"Global variables aren't necessary bad, just messy in my opinionIn what way would using a singleton be better than using a global? (Not saying it wouldn't - just wondering what your reasoning is.)2 reasons:- Always created (So you know that the object is already created)- There can only be one (of course you can change this if you need more)   ;Quote:- Always created (So you know that the object is already created)You should be able to do this with a global as well (e.g. lazy initialization).Quote:- There can only be one (of course you can change this if you need more)Why would you want to impose this restriction if it's not needed?   ;  Ok I thought there was some other C++ way.I think a few globals to a few common base objects are not so bad.And then pass pointers for all lower level classes.   ;Quote:Original post by projectghost2 reasons:- Always created (So you know that the object is already created)- There can only be one (of course you can change this if you need more)If you declare a single global instance of a type, there can only be one.Ultimately, a singleton is nothing more than a global variable inside of a namespace with some helper methods to manage initialization.   ;Quote:Original post by projectghost2 reasons:- Always created (So you know that the object is already created)And what happens when you have two of them? In what order are they always created? And what's wrong with thissomeClass instance; // yup, always createdQuote:- There can only be one (of course you can change this if you need more)Actually, no, you can't change this if you need more. And that's precisely one of the reasons singletons should be avoided like the plague.