vendredi 13 septembre 2019

Why "an inherited constructor is not a candidate for initialization from an expression of the same or derived type"?

I expected this to work

struct Parent
{
    Parent ();

    Parent (const Parent &);
};

struct Child : public Parent
{
    using Parent::Parent;
};

Parent p;

Child c (p);

Child inherits all of Parent's constructors, right?

Including Parent::Parent(const Parent &)?

x.cpp:15:11: error: no matching function for call to ‘Child::Child(Parent&)’
 Child c (p);
           ^
x.cpp:5:2: note: candidate: Parent::Parent(const Parent&)
  Parent (const Parent &);
  ^~~~~~
x.cpp:10:16: note:   inherited here
  using Parent::Parent;
                ^~~~~~
x.cpp:10:16: note:   an inherited constructor is not a candidate for initialization from an expression of the same or derived type
x.cpp:8:8: note: candidate: Child::Child()
 struct Child : public Parent
        ^~~~~

Why can't I construct a Child from a Parent?

Aucun commentaire:

Enregistrer un commentaire