For the deleted function, the compiler disables it to avoid some illegal function calls or type conversions, which improves the security of your code. This article will elaborate on the usage and benefits of defaulted and deleted functions using code examples.
defaulted function
Background questions
C + + classes have four class special member functions, namely, default constructors, destructors, copy constructors, and copy assignment operators. The special member functions of these classes are responsible for creating, initializing, destroying, or copying objects of the class. If a programmer does not explicitly define a particular member function for a class, and needs to use that particular member function, the compiler implicitly generates a default special member function for the class. For example:
Listing 1
Class x{private:int A;}; x x;
In Listing 1, the programmer does not define the default constructor for Class X, but when you create the object X of Class X, you need to use the default constructor for Class X, at which point the compiler implicitly generates a default constructor for Class X. The auto-generated default constructor has no parameters and contains an empty function body, that is, x::x () {}. Although the auto-generated default constructor has only one empty function body, it can still be used to successfully create the object X of Class X, and listing 1 can also be compiled.
However, listing 2 will have a compile error if the programmer explicitly customizes a Non-default constructor for class X without defining a default constructor:
Listing 2
Class x{public:x (int i) {a = i;} private:int A;}; x x; Error, default constructor x::x () does not exist
The reason for the compilation error in Listing 2 is that class X already has a user-defined constructor, so the compiler will no longer implicitly generate a default constructor for it. If you need to use the default constructor to create objects of the class, the programmer must explicitly define the default constructor. For example:
Listing 3
Class x{public:x () {};//Manually define default constructor X (int i) {a = i;} private:int A;}; x x; Correct, default constructor x::x () exists
As you can see from listing 3, the default constructor expected to be automatically generated by the compiler requires a programmer to write it manually, which means that the programmer's workload is increased. In addition, the Code execution efficiency of a manually written default constructor is lower than the default constructor that the compiler automatically generates. A few other classes of special member functions are also the same as default constructors, and when there is a user-defined special member function, the compiler will not automatically generate default special member functions implicitly, but requires the programmer to write manually, increasing the workload of the programmer. Similarly, the Code execution efficiency of a specially-written, special member function is less efficient than the special member function that the compiler automatically generates.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.