Sign in to follow this  

Iheritance of constructos

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am having difficulties with a following piece od code :

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! :) -----------------

Share this post


Link to post
Share on other sites
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).


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) {}
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!!

----------------

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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! :)

----------

Share this post


Link to post
Share on other sites
I'm pretty sure that the first problem arose because you do not have a default constructor in your class. This is the explaination of the error message.

Then you say that when you add it, you get weird errors. As I see it, these are generated because the singleton ctor is private. Even if you publicly derive from a class, you still cannot access to the private members. So you'll have to go the protected way (which is not really a good thing, but it is still better than having a public singleton ctor).

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
I'm pretty sure that the first problem arose because you do not have a default constructor in your class. This is the explaination of the error message.


You are 100% right, Emmanuel. I've made it just like you suggested.

Quote:
So you'll have to go the protected way (which is not really a good thing, but it is still better than having a public singleton ctor).


If you say so. Man, I am so stupid. ;) I was trying to figure out what does the abbreviaton "ctor" means. I was getting really confused since it reminded me (don't know why) of c++ streams or something. LOL :D OK, now I get it. :]

Thanks guys, bye! :)

PS. Huhu! Finally I have my game engine running. :)))

--------

Share this post


Link to post
Share on other sites

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this