Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleted constructor inheritance

One of the requirements of constructor inheritance is that the derived class can not have any constructors with the same signature. I am unsure however about how deleted functions behave under these rules.

class Foo
{
    public:
    Foo() = delete;
    Foo(const Foo& a_Foo) = delete;
    Foo(int a_Value) : m_Value(a_Value) {}

    private:
    int m_Value;
};

class Bar : public Foo
{
    public:
    using Foo::Foo;
    Bar() : Foo(7) {};
    Bar(const Bar& a_Bar) : Foo(12) {};
};
  • Are deleted constructors inherited at all?
  • If so, Bar() and Foo() have the same signature, does this make the code invalid?
  • You could argue that Foo(const Foo& a_Foo) and Bar(const Bar& a_Bar) have different signatures. How do copy constructors behave under constructor inheritance?
like image 615
Rick de Water Avatar asked Oct 27 '15 18:10

Rick de Water


People also ask

What happens to constructor in inheritance?

Constructors are not inherited. The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters to set the private instance variables of the superclass.

Are constructors and destructors inherited?

Constructors and destuctors are not members of the class and not inherited but instead automatically invoked if the sub class has no constructor. up to now this excludes c++ which supports constructor inheritance.

What is deleted copy constructor?

The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function.

Can we delete constructor in C++?

We can also use = delete to specify that you don't want the compiler to generate that function automatically. In another way, = delete means that the compiler will not generate those constructors when declared and this is only allowed on copy constructor and assignment operator.


1 Answers

Default, copy, and move constructors are not inherited, nor can inheriting a constructor implicitly declare a copy or move constructor for the derived class. Also, an inheriting constructor declaration will essentially just "skip over" a base class constructor if there's already a constructor with the same signature in the derived class.

For each non-template constructor in the candidate set of inherited constructors other than a constructor having no parameters or a copy/move constructor having a single parameter, a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears or the constructor would be a default, copy, or move constructor for that class.

([class.inhctor]/3)

Also, an inherited constructor is deleted if the corresponding base class constructor is deleted.

A constructor so declared has the same access as the corresponding constructor in X. It is deleted if the corresponding constructor in X is deleted (8.4). An inheriting constructor shall not be explicitly instantiated (14.7.2) or explicitly specialized (14.7.3).

([class.inhctor]/4)

like image 105
Brian Bi Avatar answered Oct 07 '22 16:10

Brian Bi