class CGameEngine : public COpenGLWindow, public Singleton <CGameEngine>
{
private:
(...)
public:
(...)
}
The problem is that I don't know how to specify CGameEngine constructor in the way that COpenGLWindow class' constructor will also be used.
I guess, that's why I keep getting a following error (btw MSVC 6.0) :
CGameEngine' : no appropriate default constructor available
f:\microsoft visual studio\myprojects\jadventures\singleton.h(28) : while compiling class-template member function 'class CGameEngine &__cdecl Singleton<class CGameEngine>::Instance(void)'
What can I do about it?
Thanks for any help! :)
-----------------
Iheritance of constructos
Hello!
I am having difficulties with a following piece od code :
Constructors are not inherited. If you want your derived class to have a constructor similar to its base class, you need to write it out.
If you want to use a base class constructor other than the default, parameterless, constructor, you need to pass the parameters via the initializer list (the same place where you should put member variable initializations).
If you want to use a base class constructor other than the default, parameterless, constructor, you need to pass the parameters via the initializer list (the same place where you should put member variable initializations).
class Foo{public: Foo(int, int, double, char) {}};class Bar : public Foo{public: Bar(int i, double d, char c) : Foo(i, 42, d, c) {}};
you would do something like
edit
damn I'm way too slow
CGameEngine():CopenGLWindow()//initialise base object{//do stuff here}
edit
damn I'm way too slow
Finally, declaring public constructor for the template singleton class solved the problem.
-------------
-------------
Quote:Original post by clapton
Finally, declaring public constructor for the template singleton class solved the problem.
-------------
That doesn't sound good to me, a singleton should have its constructor, destructor, copy constructor and assignment operators private (or protected if other classes are going to inherit from them, like in this case). This then stops the rest of the code creating the object and forces it to use the Instance() function, which then ensures there is only ever one instance of the object.
Can you post some code for the Instance() function? That seems to be where the problem is.
Quote:Can you post some code for the Instance() function? That seems to be where the problem is.
Sure I can. Here is a whole Singleton class :
template <typename MyClass> class Singleton{ private: static MyClass *m_pSingleton; ~Singleton() // Destructor { if (m_pSingleton) delete m_pSingleton; } Singleton ( MyClass &other ) { } // No copy constructor Singleton operator= ( MyClass &other ) { return this; } // Assingment operator // Private address operators MyClass* operator&() { return this; } const MyClass* operator&() const { return this; } public: Singleton () {} // Constructor static MyClass& Instance() // Get class instance { if (m_pSingleton == NULL) m_pSingleton = new MyClass(); return *m_pSingleton; }};template <typename MyClass> MyClass* Singleton <MyClass>::m_pSingleton = 0;
I also was a bit concerned about making the constructor public. Although I tried how does it work and everything seemed pretty fine.
Thanks!!
----------------
Right, so the problem is with this line:
if (m_pSingleton == NULL) m_pSingleton = new MyClass();
The compiler is saying it doesn't know how to create MyClass taking no arguments. You need to create a default constructor (a constructor that takes no arguments) for MyClass (in this case CGameEngine).
In CGameEngine, if you need to call a constructor of an inherited class (COpenGLWindow in this case) you can do this:(as stated by CoffeeMug)
CGameEngine():COpenGLWindow() //Call base constructor
{
//Do rest of stuff
}
Also, as a tip, you should make the constructor, destructor, copy constructor and assignment operators of your CGameEngine class private and make Singleton<CGameEngine> class a friend, as this will stop you createing a CGameEngine object anywhere in your code and forces the use of Instance().
Another tip is to make your copy contructor and assignment operators take a constant references as a parameter, I can't remeber why now but I read it somewhere, think it helps the compiler or something.
Hope that helps.
if (m_pSingleton == NULL) m_pSingleton = new MyClass();
The compiler is saying it doesn't know how to create MyClass taking no arguments. You need to create a default constructor (a constructor that takes no arguments) for MyClass (in this case CGameEngine).
In CGameEngine, if you need to call a constructor of an inherited class (COpenGLWindow in this case) you can do this:(as stated by CoffeeMug)
CGameEngine():COpenGLWindow() //Call base constructor
{
//Do rest of stuff
}
Also, as a tip, you should make the constructor, destructor, copy constructor and assignment operators of your CGameEngine class private and make Singleton<CGameEngine> class a friend, as this will stop you createing a CGameEngine object anywhere in your code and forces the use of Instance().
Another tip is to make your copy contructor and assignment operators take a constant references as a parameter, I can't remeber why now but I read it somewhere, think it helps the compiler or something.
Hope that helps.
Quote:Original post by desertcube
The compiler is saying it doesn't know how to create MyClass taking no arguments. You need to create a default constructor (a constructor that takes no arguments) for MyClass (in this case CGameEngine).
In CGameEngine, if you need to call a constructor of an inherited class (COpenGLWindow in this case) you can do this:(as stated by CoffeeMug)
Well, I've tried it (even before posting on the forum) and I got some other weird error. :/
Quote:
Also, as a tip, you should make the constructor, destructor, copy constructor and assignment operators of your CGameEngine class private and make Singleton<CGameEngine> class a friend, as this will stop you createing a CGameEngine object anywhere in your code and forces the use of Instance().
OK, I'll try it. I didn't know that base class can be also a friend. :)
Quote:
Another tip is to make your copy contructor and assignment operators take a constant references as a parameter, I can't remeber why now but I read it somewhere, think it helps the compiler or something.
Actually I just forgot to do that. Anyway, I was 100% sure that someone will mention it. ;)
Quote:
Hope that helps.
Sure it does! Great thanks! :)
----------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement