1 =default Constructor Qualifier
C++11 provides a =default qualifier for constructors that you can use to explicitly specify that the compiler automatically generates specific constructors, destructors, or assignment operations. The reference code is as follows:
class ctordfttype{public: ctordfttype ()=default; Ctordfttype (const ctordfttype&) =default; operator = (const Ctordfttype &) =default; ~ctordfttype () =default;};
Constructors that use the =default qualifier do not need to be implemented, and the compiler automatically generates a default function implementation.
2 =delete function Qualifier
If you need to suppress replication or assignment between class objects, it is a traditional practice to set the copy constructor and assignment operators to private. The =delete qualifier provided by C++11 is used to implement similar functions, and the =delete qualifier can be used for any function, primarily for access control of class member functions.
The reference code is as follows:
class delmodtype{public: delmodtype ()=default; Delmodtype (const delmodtype&) =Delete; // no copy ctor operator = (const delmodtype&) =Delete// no assignment default;};
It is necessary to note that =delete can be used for ordinary member functions. It is recommended that destructors do not use the =delete qualifier.
The =delete qualifier is passed, causing the auto-generated constructors, destructors, and assignment operators of the classes that contain, reference the member to become private. (This private constructor one meaning)
The new standard does not recommend the use of the private constructor, the assignment operator form.
3 Explicit conversion operators
C++11 the overloading of overloaded type conversion operators with ease of misuse and misuse, introduces an overload of the explicit qualifier to describe the conversion operator, which must be displayed before the conversion can be made. The sample code is as follows:
class explicitconversion{public: explicitoperator bool Const{returntrue;}};
4 Override member function qualifier
C++11 provides an override qualifier, which is used to display an interface in a subclass that is inherited from the parent class, and is overridden in a subclass. Its usage is as follows:
classbase{ Public: Base ()=default; Virtual~base () =default; Virtual intGetType ()Const{return 0;}};classDerived: Publicbase{ Public: Derived ()=default; ~derived () =default; intGetType ()Const Override{return 1;}};
5 Final keyword
C++11 introduces the final keyword to decorate the class name, indicating that the class cannot be a base class or a parent class.
class noderived final{};
Final can also be used to modify member functions to represent member functions that cannot be overridden.
6 Inherited constructors
C++11 introduces inherited constructors, although they are inherited, but usage is not the same as the actual meaning. This mechanism is introduced using a using declaration. The code is as follows:
class inbase{public: inbase (intint y): m_x (x), m_y (y) {} virtual ~inbase () {}protected: int m_x, m_y;}; class Public inbase{public: using inbase::inbase;};
The subclass Inderived constructor is equivalent to the following code:
class Public inbase{public: inderived (intint y): inbase (x, y) {}};
That is, the inherited constructor indicates that the subclass has a constructor that has the same parameters as the parent class. It is important to note that inherited constructors do not support inheritance of default parameters.
7 Rvalue Reference Rval Reference and move constructor moves Ctor, move assignment operator move Assignment
The so-called rvalue reference refers to the object that is about to be destroyed, with only one pointer or reference pointing to the object, more common such as literals, variables that are about to be destroyed in the stack. Similar rvalue references if there are pointers or other dynamically allocated resources, we can use the move constructor or the move assignment operator to reuse its resources and empty the original pointer. In view of the more content, the recommendations that you would like to learn more about refer to C++primer ch13.6 or c++11 standards.
Additional Instructions
This article is mainly about C + + Primer Fifth edition of the 13th chapter to the 15th chapter on the c++11 content of the collation. Includes the default, delete, explicit, override, final and other keywords, inherited constructors, mobile constructors and other mechanisms.
All the code is compiled in the GCC v4.8.1 version of the test, the relevant source code can be downloaded from my git, the URL is as follows: Https://git.oschina.net/Tocy/SampleCode.git, located in the c++11 directory Cpp_ The Primer_test3.cpp file.
C + + Primer reading notes of C++11 (iii)