First time with singletons
Hi, I need help with singletons. I would like to have access from any class in my program to every singletons. So, by exemple, I would like to have a class, which has an Object in it, and this object also has an object. So, right now, I''m only trying this:
----singleton.h-----
#ifndef...#define
#include "object1.h"
class Singleton
{
CObject1 Obj1;
Singleton();
public:
Singleton& Instance()
{
static Singleton obj;
return obj;
}
}
#endif
----object1.h----
#ifndef...#define
#include "object2.h"
//a simple class which has an "Object2" in it...
----object2.h----
#ifndef...#define
#include "singleton.h"
//a simple class which must access to the singleton
The problem is that singleton include object1, which includes object2, which includes singleton... What can I do??? Right now, I get a "missing '';'' before identifier..." error in my object2.h class.
Thanks for your help!
in object2.h, don''t #include singleton.h. Just declare the singleton as a class. That''s enough, if your header only includes pointers and references to the singleton (which, since it''s a singleton, are all it can have).
How appropriate. You fight like a cow.
How appropriate. You fight like a cow.
Listen to the compiler, you forgot a ;
Regards, VizOne
#ifndef...#define#include "object1.h"class Singleton{CObject1 Obj1;Singleton();public:Singleton& Instance(){static Singleton obj;return obj;}}; <-- was missing!#endif
Regards, VizOne
No, I didn''t forget to put a '';''.
The first problem was that I was not defining the class, like:
#include "singleton.h"
class CSingleton; <-WAS MISSING!
But now, i have a different error in the class on the following line:
CSingleton pSingleton;
->ERROR: ''pSingleton'' uses undefined class ''CSingleton''
Thanks for your help!
The first problem was that I was not defining the class, like:
#include "singleton.h"
class CSingleton; <-WAS MISSING!
But now, i have a different error in the class on the following line:
CSingleton pSingleton;
->ERROR: ''pSingleton'' uses undefined class ''CSingleton''
Thanks for your help!
no offence but this design is sort of a OO "rookie" mistake. Of course you may be working on a small private project which means feel free to design what you want. ;-)
But the only real practical use of a singleton is to restrict access, and provide creation control of critical data structures. Your design provides "free for all" access. Which would be bad in much the same ways as making all data in your program global. The whole point of using encapsulation (classes) is to localize data and algorithms so that if (or when) they need to change, you change code in the least amount of places possible.
I know from experience because on one of my first projects I worked on professionally, I essentially did what you described. All was fine until the specs for the project changed... (and someone will always change them before your done) and man did I have to re-write a lot of crap :-)
But the only real practical use of a singleton is to restrict access, and provide creation control of critical data structures. Your design provides "free for all" access. Which would be bad in much the same ways as making all data in your program global. The whole point of using encapsulation (classes) is to localize data and algorithms so that if (or when) they need to change, you change code in the least amount of places possible.
I know from experience because on one of my first projects I worked on professionally, I essentially did what you described. All was fine until the specs for the project changed... (and someone will always change them before your done) and man did I have to re-write a lot of crap :-)
It''s only a little project, but it''s the way I prefer, I don''t want to use global objects for this project.
So how do I solve my problem?
Thanks a lot!
So how do I solve my problem?
Thanks a lot!
read enginuity part 2.
Also, you Singleton should be nothing but a singleton (it shouldn''t depend on CObject1). The singleton should have no external dependancies. This eliminates the blah.h includes singleton.h which includes blah.h problem.
"I would like to have access from any class in my program to every singletons."
That could get really messy really fast.
Also, you Singleton should be nothing but a singleton (it shouldn''t depend on CObject1). The singleton should have no external dependancies. This eliminates the blah.h includes singleton.h which includes blah.h problem.
"I would like to have access from any class in my program to every singletons."
That could get really messy really fast.
The "blah.h" include problem was caused by a misunderstanding of how #include works (copy paste). Knowing this, I now only include my "singleton.h" file in the cpp files and everything works fine. For this little project, it will be really interesting to have access to the "root" of my objects "tree".
We must all pass through this kind of experience. I''m pretty sure everybody tried someday to program 100% OO, this is nearly what I''m trying. And don''t tell me to not do so! I must learn by my own mistakes .
Anyway, my idea of "root" singleton is working well. But I would like to know what you would have done with such a project? I have a CGame main object, which includes a CNetwork object, a vector of CPlayers, etc., the network incluse a CServer, CClient, CMessenger... If this is not the best way I should do things. What is wrong for such a little project? This is nothing professionnal, only a try! I''ll take your suggestions in consideration for my next projects.
Thanks a lot!
We must all pass through this kind of experience. I''m pretty sure everybody tried someday to program 100% OO, this is nearly what I''m trying. And don''t tell me to not do so! I must learn by my own mistakes .
Anyway, my idea of "root" singleton is working well. But I would like to know what you would have done with such a project? I have a CGame main object, which includes a CNetwork object, a vector of CPlayers, etc., the network incluse a CServer, CClient, CMessenger... If this is not the best way I should do things. What is wrong for such a little project? This is nothing professionnal, only a try! I''ll take your suggestions in consideration for my next projects.
Thanks a lot!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement