gpt4 book ai didi

c++ - 为什么具有用户声明的析构函数的类具有隐式默认的构造函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:18:42 25 4
gpt4 key购买 nike

代码:

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

A::A(){ }; //error: definition of implicitly declared default constructor

int main()
{
A a;
}

DEMO

为什么代码会产生错误?我希望程序编译正常。标准说 N3797::12.8/7 [class.copy]:

If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

是bug还是我误会了?

最佳答案

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

A::A(){ }; //here you can define default constructor

int main()
{
A a;
}

你已经定义了显式析构函数而不是构造函数,添加构造函数声明并将其定义为外部类

关于c++ - 为什么具有用户声明的析构函数的类具有隐式默认的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26667975/

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