gpt4 book ai didi

c++ - 类中已删除的析构函数显示为虚拟/直接基类或一种非静态数据成员

转载 作者:太空宇宙 更新时间:2023-11-04 13:43:22 28 4
gpt4 key购买 nike

关于复制/移动构造函数被隐式删除的情况有一个规则:

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

[...]

因为找不到反射(reflect)规则的例子,所以不是很清楚。考虑以下代码:

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

因为删除的移动构造函数不参与重载决策,我预计错误会类似于“复制构造函数被隐式删除”。但相反,我得到了关于已删除的 B() 的错误,这是我明确定义的。您不能提供一个反射(reflect)该规则的示例吗?

最佳答案

仅根据您提供的摘录,以下是示例:

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

struct outer
{
inner inst;

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

关于c++ - 类中已删除的析构函数显示为虚拟/直接基类或一种非静态数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923320/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com