简体   繁体   中英

Why does a class with a user-declared destructor have an implicitly defaulted constructor?

Code:

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

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

int main()
{
    A a;
}

DEMO

Why does the code produces the error? I expected that the program compiles fine. The Standard says 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 .

It's a bug or my misunderstanding?

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

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

int main()
{
 A a;
}

you have defined explicit destructor not constructor , add constructor declarartion and define it as outside clas

You may not define an implicitly declared constructor by the compiler.

From the C++ Standard (12 Special member functions)

  1. ... Programs shall not define implicitly-declared special member functions

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