| Start |
|---|
| Default Constructor A default constructor is a type of constructor that requires no parameters when called. It is named after the type it constructs and is a member function of it (as all constructors are). Another way to satisfy the "no parameters" requirement is for the developer to provide default values for all parameters: Under some circumstances (i.e., the developer provides no constructors and there are no other disqualifying conditions), the compiler implicitly provides an empty default constructor: Having some other type of constructor is one of the disqualifying conditions mentioned earlier: To prevent implicit default constructor creation, a common technique is to declare it as To be sure a default constructor (functionally similar to the implicit one) is defined, a developer could write an empty one explicitly. In C++11, a developer can also use the Furthermore, a developer may also be explicit about wanting the compiler to provide a default constructor. You can determine whether a type has a default constructor (or is a primitive type) using In C++11, it is still possible to use the non-functor version of |
| Copy and swap If you're writing a class that manages resources, you need to implement all the special member functions (see Rule of Three/Five/Zero (opens new window)). The most direct approach to writing the copy constructor and assignment operator would be: But this approach has some problems. It fails the strong exception guarantee - if To satisfy the strong exception guarantee and avoid code duplication (double so with the subsequent move assignment operator), we can use the copy-and-swap idiom: Why does this work? Consider what happens when we have First, we copy-construct The above formulation works as-is already for move assignment. Here, we move-construct |
| Destructor A destructor is a function without arguments that is called when a user-defined object is about to be destroyed. It is named after the type it destructs with a Under most circumstances (i.e., a user provides no destructor, and there are no other disqualifying conditions), the compiler provides a default destructor implicitly: In C++11, a developer can override this behavior by preventing the compiler from providing a default destructor. Furthermore, a developer may also be explicit about wanting the compiler to provide a default destructor. You can determine whether a type has a destructor (or is a primitive type) using |
| Implicit Move and Copy Bear in mind that declaring a destructor inhibits the compiler from generating implicit move constructors and move assignment operators. If you declare a destructor, remember to also add appropriate definitions for the move operations. Furthermore, declaring move operations will suppress the generation of copy operations, so these should also be added (if the objects of this class are required to have copy semantics). |
| Virtual and Protected Destructors A class designed to be inherited-from is called a Base class. Care should be taken with the special member functions of such a class. A class designed to be used polymorphically at run-time (through a pointer to the base class) should declare the destructor If the class does not need to be polymorphic, but still needs to allow its interface to be inherited, use a non-virtual Such a class can never be destroyed through a pointer, avoiding silent leaks due to slicing. This technique especially applies to classes designed to be Finally, some classes may require that they are never used as a base class. In this case, the class can be marked |
Read Article
https://develop.horje.com/learn/1434/reference