Jump to content
  • Advertisement
Sign in to follow this  
Eugenek

Passing parameter to base class constructor

This topic is 4390 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

I'm trying to pass a parameter to a base class's constructor but my compiler is complaining that it can't find the base class constructor. Below is a basic example of what I'm trying to do. I've omitted the implementation of the constructors.

class Parent
{
    public:
        Parent(int);
};

class Child : public Parent
{
    public:
        Child(int);
};  

int main()
{
    Parent* pChild = new Child(42);
}


The compiler is looking for Parent() rather than Parent(int). If I create the Parent() constructor it compiles but the parameter never makes it to the base class constuctor. Is it possible to sent a parameter to a base class constructor? Thanks, Eugenek

Share this post


Link to post
Share on other sites
Advertisement
So, you want us to tell you what you did wrong in your constructors, but you think it's okay to omit the implementation of said constructors? Remember, if you have a bug you don't understand, you probably aren't able to reasonably pass judgement on what portions of the code are relevant and which are not. Consequently, you should err on the side of caution and post more code rather than less.

To invoke a base class constructor from a derived class, you call the constructor from the member initialization list of the derived class, as follows:


class Parent
{
public:
Parent(int)
{
/* whatever... */
}
};

class Child : public Parent
{
public:
Child(int a)
: Parent(a)
{
}
};

int main()
{
Parent* pChild = new Child(42);
}


Note that since you (probably) did not do that in your original code, the compiler tried to default-construct the base portion of the child object, which it could not do since you suppressed generation of the default Parent constructor by provided a parameterized version.

Share this post


Link to post
Share on other sites
I think this part is what you were looking for:

Child(int a)
: Parent(a)
{
}

You have to explicitly call the non-default constructor in the derived class's constructor(s).

Greetz,

Illco

Share this post


Link to post
Share on other sites
Thanks. That did the trick.

I apologize for not including the implementation of the constructors. They were trivial to me since they could simply just "cout" the parameter for my demostration purposes. However, I should have including them nevertheless so it could be seen that I wasn't doing what I should have been doing.

Thanks again.

Share this post


Link to post
Share on other sites
For future reference: it is usual to try to trim one's problem down to a *minimum, complete* code sample that reproduces the problem. So for example, if a problem with constructors doesn't depend on the constructor contents, then you would confirm that the problem is reproduced with empty constructors, and post a code sample with empty contructors (here you could just put {}'s instead of the ;'s). [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!