简体   繁体   中英

Deleted destructor in the class appeared as a virtual/direct base class or as a type of non-static data member

There is a rule about cases when the copy/move constructor is implicitly deleted:

An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has:

[...]

— any direct or virtual base class or non-static data member of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or

[...]

Because I can't find an example reflecting the rule, it's not clear to me. Consider the following code:

struct A
{
    ~A() = delete;
};

struct B : A
{
    A a;
    B(){ }; //error error: attempt to use a deleted function B(){ };
    B(const B&&)  = delete;
};

B *b = new B;

int main() { }

DEMO

Because of deleted move constructor doesn't take a part in overload resolution, I expected the error would be something like "Copy constructor is implicitly deleted". But instead I got the error about deleted B() , which I defined explicitly. Couldn't you provide an example reflecting that rule?

Based only on the excerpt you've provided, the following is an example:

struct inner
{
    ~inner() = delete;
};

struct outer
{
    inner inst;

    // Can't destroy "inst"; outer now has an implicitly
    // deleted destructor and copy/move constructor.
};

Look at 5th point: it is clearly saying that you have deleted your base class dtor so you are having this problem.

link: http://en.cppreference.com/w/cpp/language/default_constructor

Deleted implicitly-declared default constructor

The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:

  1. T has a member of reference type without a brace-or-equal initializer. (since C++11)

  2. T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11).

  3. T has a member (without a brace-or-equal initializer) (since C++11), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.

  4. T has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.

  5. T has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.

  6. T is a union with at least one variant member with non-trivial default constructor. (since C++11)

  7. T is a union and all of its variant members are const.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM