Overview
In C++ 11, a class can define following special member functions.
- default constructor
- copy constructor
- copy assignment operator
- destructor
- move constructor
- move assignment operator
If it's missing, the compiler conditionally generates some of these functions.
Sometimes it's desirable to explicitly generate these functions, when it's not generated by the compiler using keyword =default or suppress conditional generation of these functions by using keyword =delete.
Details
=default
As discussed earlier, following rules apply for the automatic generation of special functions by the compiler.
Default constructor
Generated only if the class contains no user-declared constructors.
Destructor
Generated only if the class contains no user-declared destructor. virtual only if a base class destructor is virtual.
copy constructor
Performs memberwise copy construction of non-static data members. The copy constructor is generated only for classes lacking an explicitly declared copy constructor, and it’s deleted if a move operation is declared.
copy assignment operator
Performs memberwise copy of non-static data members. The copy assignment operator is generated only for classes lacking an explicitly declared copy assignment operator, and it’s deleted if a move operation is declared. Generation of the copy operations in classes with an explicitly declared destructor is deprecated.
move constructor and move assignment operator
Move operations are generated only for classes lacking explicitly declared move operations, copy operations, and a destructor.
In the following case, the class defines a constructor with a parameter. The compiler throws error if the default constructor is not defined.
struct person { person(string name):name(name){}; string name; };
person p; //error no default constructor
Therefore, a default constructor needs to be generated as shown below.
struct person { person(string name):name(name){}; person()=default; string name; };person p; //no error
The example 9 depicts this scenario.
=delete
In some cases compiler generated special functions are undesirable. For example, classes such as ios_base suppress use of copy constructor and copy assignment operators by making them private as shown below. This is to prevent instantiated objects to be cloned or copy constructed.
struct person { person(string name):name(name){}; person()=default; private: person(const person& p)=default; person& operator =(const person& p)=default;string name;} p, p2;person p3 = p; //error cannot constructp2=p; //error cannot clone
instead , they can be simply suppressed by using =delete keyword as shown below.
struct person { person(string name):name(name){}; person()=default; person(const person& p)=delete; person& operator =(const person& p)=delete; string name;} p("test"), p2("test2");;person p3 = p; //error cannot constructp2=p; //error cannot clone
Another case would be to suppress implicit conversions in user defined functions as shown below.
Here passing bool value to function update is prevented.
struct person { person(string name):name(name){}; person()=default; void update (int age){age=age;} void update (bool age)=delete; person(const person& p)=delete; person& operator =(const person& p)=delete; string name; int age; } p("test");p.update(true); //error function deleted
The example 10 depicts this scenario.
No comments:
Post a Comment