Jump to content
  • Advertisement
Sign in to follow this  
SymLinked

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

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

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
Advertisement
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
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
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!