Sign in to follow this  
SymLinked

[C++] Is there a way to let a class use its parent class's constructor?

Recommended Posts

SymLinked    1233
Hi, If I create a class (C) with another class (P) as parent, how do I let class (C) use (P)'s constructor? All functions work that way, but not the constructor.

Share this post


Link to post
Share on other sites
the_edd    2109
Quote:
Original post by SymLinked
Hi,

If I create a class (C) with another class (P) as parent, how do I let class (C) use (P)'s constructor? All functions work that way, but not the constructor.


IIRC, this facility will be available in the upcoming revision of the C++ standard. For now, you will have to create a constructor in your derived class that forwards on its arguments :(

OTOH, some compilers already parts of the new standard. Maybe yours does? I think the proposed syntax was something like:


class Derived : public Base
{
public:
using Base::Base;

//...
};

Share this post


Link to post
Share on other sites
SymLinked    1233
I was hoping it would be "automatic" just like other functions are so there's no additional typing when deriving from the parent. Something like this:


class Parent
{
Parent (const String &name);
};

class Child: public Parent
{
// Whatever
};

Child child ("This is my name!");



Thanks for the tips though!

Share this post


Link to post
Share on other sites

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