0

I have an abstract base class that needs some objects passed to its constructor to initialize its members. But I would like to get rid of passing those objects through the derived class constructor.

class Derived : public Base
{
public:
    Derived(type one, type two, type three) : Base(one, two, three)
    {
        // ...

The objects passed to the base classes are the same instances for all created derived classes. Is there a way to bind them to the base class' constructor, so that I don't have to forward them through the derived class' constructor?

// do some magic
// ...

class Derived : public Base
{
    // no need for an explicit constructor anymore
    // ...
danijar
  • 32,406
  • 45
  • 166
  • 297
  • Are you sure `std::bind` has got anything to do with this? – jrok Aug 18 '13 at 16:59
  • `std::bind` allows to bind values to function parameters. I am looking for something similar for class constructors of base classes. – danijar Aug 18 '13 at 17:24

1 Answers1

1

In C++11 you can inherit constructors to the base class. It seems this would roughly do what you need:

class derived
    : public base {
public:
    using base::base;
    // ...
};
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Could you please provide an example of how to construct a derived class while passing the parameters to the base class using inherited constructors? – danijar Aug 18 '13 at 17:57
  • @danijar: I don't think I understand your request: you'd create a derived object passing it the arguments the base class takes. If you refer to implementing the derived classes's constructor, you wouldn't implement it at all! If you need a custom initialization in the derived class you could factor it into the construction of a derived member. – Dietmar Kühl Aug 18 '13 at 18:06