简体   繁体   English

该类中已删除的析构函数显示为虚拟/直接基类或非静态数据成员的类型

[英]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. 隐式声明的copy / move构造函数是其类的内联公共成员。 A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has: 如果X具有以下功能,则默认将类X的复制/移动构造函数定义为已删除(8.4.3):

[...] [...]

— 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 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". 因为删除的move构造函数不参与重载解析,所以我预计该错误将类似于“ Copy构造函数被隐式删除”。 But instead I got the error about deleted B() , which I defined explicitly. 但是相反,我得到了有关删除的B()的错误,我明确定义了它。 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. 看第5点:显然是您已经删除了基类dtor,所以遇到了这个问题。

link: http://en.cppreference.com/w/cpp/language/default_constructor 链接: 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: 如果满足以下任一条件,则类T的隐式声明或默认的默认构造函数未定义(直到C ++ 11)定义为已删除(从C ++ 11开始):

  1. T has a member of reference type without a brace-or-equal initializer. T具有引用类型的成员,而没有大括号或相等的初始化程序。 (since C++11) (自C ++ 11起)

  2. T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11). T有一个const成员,没有用户定义的默认构造函数或大括号或相等的初始化程序(自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. T有一个成员(没有花括号或等于的初始值设定项)(自C ++ 11起),该成员具有已删除的默认构造函数,或者其默认构造函数不明确或无法从此构造函数访问。

  4. T has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor. T具有直接或虚拟基数,该基数具有已删除的默认构造函数,或者从该构造函数不明确或不可访问。

  5. T has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor. T具有直接或虚拟基础,该基础具有已删除的析构函数,或无法从此构造函数访问的析构函数。

  6. T is a union with at least one variant member with non-trivial default constructor. T是具有至少一个具有非平凡默认构造函数的变体成员的联合。 (since C++11) (自C ++ 11起)

  7. T is a union and all of its variant members are const. T是一个联合,其所有变体成员都是const。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 枚举不是类的非静态数据成员或基类 - enum is not a non-static data member or base class of class class 类型的非静态数据成员的声明也是定义吗? - Is a declaration for a non-static data member of a class type also a definition? 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 成员初始化程序“ SuperClass”未命名非静态数据成员或基类 - member initializer 'SuperClass' does not name a non-static data member or base class 错误消息:在人员类型的多个基类子对象中找到非静态成员 - error message: non-static member found in multiple base-class subobjects of type person “没有与第一个非静态数据成员相同类型的基类” - “no base classes of the same type as the first non-static data member” 模板类和继承问题-“列表”未命名非静态数据成员或基类 - Issues with template classes and inheritance - 'List' does not name a non-static data member or base class 类到模板错误:无效使用非静态数据成员 - Class to template error: invalid use of non-static data member 基类中受保护的非虚拟析构函数 - Protected non-virtual destructor in the base class 澄清指向非静态类成员的指针 - Clarification on pointer to non-static class member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM