c++ - Why would you "default" a copy/move constructor or a destructor? -


C ++ 0x lets you specify some functions as default:

  Struct A {A () = default; // default ctor A (a const & amp;) = default; // copy AT (A & amp;) = default; // step ctor A (other); // other ctor ~ A () = default; // dtor A & amp; Operator = (a const & amp;) = default; // copy assignment a & amp; Operator = (A & amp;) = default; // Assign the steps};  

The implementation of these functions is similar, as if the compiler has created them, something that is usually done in most situations when you do not declare your own.

A If you declare any ctor (any other above) then the default CTOR is not not generated, so you have to default to get it back. Could.

However, the base or data member prevents them, a class always has a copy and ctor⁠- and if they are precluded, the default implementation will not work. A class always has a DTOR.

Why would you have to clear a copy CTOR, CTOR or the district clearly? Does the implementation of implications do not do the same thing, anyway?

You may need to do this to change their access to non-public or those units What control unit determines what to control.

Non-public

Although these functions are generally public, you want them non-public and still want the default implementation:

  struct A { Protected: ~ A (); Private: A (); A (a const & amp;); A & amp; amp;; }; According to // N3092, §8.4.2 / 2, can not be non-public and default // A: ~ A () = default in the class definition; A :: A () = default; A :: A (a const & amp;) = default; A :: A (A & amp;) = default;  

This category can be created, copied and transferred by default, but by methods and friends of A, only is useful for factories where Build can be tightly controlled.

The second part of a protected district school is:

Guideline # 4: A base class destroyer should either be public and virtual or protected and non-virtual

Definition control

Additionally, the default function can be used to maintain a stable binary interface, because the default control function is defined on the control is. By default, inline does not have the meaning, because contained version will be declared. (In the above code, the default functions should not be either in the title or inline specifier should be added.)


Comments