Can I std::bind a base class' constructor so that the derived doesn't need to call it explicitly?

StackOverflow https://stackoverflow.com/questions/18301519

Pregunta

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
    // ...
¿Fue útil?

Solución

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;
    // ...
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top