Why is "subclass copy constructor calling virtual inherited parent trivial constructor" not recommended

Viewed 66

I have a class that virtually inherits from a base class that only has a trivial constructor, but the copy constructor is explicitly deleted. Now I need to provide a copy constructor for this subclass and call the trivial constructor of the base class, like this:

class A {
protected:
    explicit A () {}
    A (A const&) = delete;
    virtual ~A () {}
};
class B : virtual public A {
protected:
    explicit B () {}
    B (B const&): A () {} 
    virtual ~B () {}
};
class C : public B {
public:
    explicit C () noexcept {}
    C (C const&) noexcept
      try: B () {} catch (...) { }
    ~C () noexcept {}
};
int main () {
    C c1;
    C c2{c1};
    return 0;
}

There is no warning when I compile with godbolt.org ( https://godbolt.org/z/q6eWTj7vP ). Under "Dev C++ 5.11, TDM-GCC 4.9.2 64-bit Release", if I only use the compilation option -std=c++11 to compile, there will be no error. However, after the compilation option -Wextra is added, a warning is generated (in class C):

17  5   C:\Users\test.cpp   [Warning] base class 'class A' should be explicitly initialized in the copy constructor [-Wextra]

When I delete the keyword 'virtual' in class B : public virtual A { in the above code, the warning disappears.

I can't ignore any warning message, so I wonder why this warning message is generated. Thanks.

0 Answers
Related