New usage of C + + default and delete

Source: Internet
Author: User

Default functions in C + + and defaults and delete usages

First, the default function in the class
A. Default member functions in a class
1. Default constructors
2. Default destructor
3. Copy Constructors
4. Copy Assignment function
5. Move Constructors
6. Move copy function

B. Custom operator functions in a class
1.operator
2.operator&
3.operator&&
4.operator*
5.operator->
6.operator->*
7.operator New
8.operator Delete

At the same time, C + + stipulates that once a programmer implements a custom version of these functions, the compiler will no longer automatically produce the default version. Note that the default version is not generated automatically, or you can manually generate the default version. When we define our own constructor for arguments, it is better to declare a version without parameters to complete the parameterless variable initialization, where the default version is no longer automatically provided by the compiler. We can control the generation of the default constructor by using the keyword default, which explicitly instructs the compiler to generate the default version of the function. Like what:
Class MyClass
{
Public
MyClass () =default; Both the default version and the version with the parameter are available, and the type is pod
MyClass (int i):d ata (i) {}
Private
int data;
};

There are times when we want to limit the generation of default functions. It is typical to prohibit the use of copy constructors, which was previously done by declaring copy constructors as private and not providing implementations, so that when a copy constructs an object, the compilation does not pass, and c++11 uses the DELETE keyword to explicitly instruct the compiler not to generate the default version of the function. Like what:
Class MyClass
{
Public
MyClass () =default;
MyClass (const myclass&) =delete;
......
}

Of course, once the function has been deleted, overloading the function is also illegal, which we are accustomed to call a delete function.

Ii. other uses of default and delete
Above we have seen in the class we can use the default and delete modifier member function, make it a default function or delete a function, outside the class, default may be outside the class definition to modify the member functions, such as:
Class MyClass
{
Public
MyClass () =default;
MyClass () &operator= (const myclass&);
);
In the definition of the class, use default to indicate the default function version
Inline myclass& myclass::operator= (const myclass&) =default;
It is also possible to myclass& myclass::operator= (const myclass&) =default, but the members of this class outside the definition composition are not inline functions.
Unlike =default, =delete must appear in the first declaration of a function. Because a default member only affects the code generated for this member, =default is not required until the code is compiled, and the compiler must know early whether a function is deleted in order to prohibit the operation that attempts to use it.

In general, destructors cannot be defined as deleted, because objects of this type cannot be deleted if the destructor is deleted. For a type that has a destructor removed, the compiler will not allow the definition of a variable of that type or create a temporary object of that type, and if a class has a type that has a member that deletes a destructor, it cannot define a variable or temporary object of that class, because a member's destructor is deleted, and the member cannot be destroyed. The class that contains it cannot be destroyed. Although the type of destructor is removed, we cannot define variables or members of this type but can dynamically allocate objects of this type, such as:
Struct nodtor{
Nodtor () =default;
~nodtor () =default;
};
Nodtor *p=new nodtor ();//correct, but I can delete P
But sometimes destructors can also be deleted, so that we do not need destructors to perform object-level cleanup when we allocate memory at the specified memory location, and we can display delete destructors to restrict the construction of custom types on the stack or statically.

Explicit deletion of delete is not limited to member functions, such as:
void Func (int i) {};
void Func (char c) =delete; Explicitly delete char versions
int main ()
{
Func (3);
Func (' C '); Failed to compile through
return 0;
}
This is because the char version of Func has been deleted, so func (' C ') fails to compile. From this we also know that default is limited to the partial member function of the class. We can also use Delete to avoid unnecessary implicit data-type conversions. Like what:
Class MyClass
{
Public
MyClass (int i) {};
Myclsss (char c) =delete; To delete a char version of a constructor
};
void Fun (MyClass m) {}
int main ()
{
Func (3);
Func (' a '); Compilation does not pass
MyClass M1 (3);
MyClass m2 (' a '); Compilation does not pass
}
This is because the way to construct a MyClass object from Char is disallowed after the Char version's constructor is removed. But after removing this function, the compiler implicitly converts a to integer to make the compilation pass, calling the integer constructor, which may not be what you want.

But if so:
Class MyClass
{
Public
MyClass (int i) {};
Explicit myclsss (char c) =delete; Remove the constructor for the char version of explicit
};
void Fun (MyClass m) {}
int main ()
{
Func (3);
Func (' a '); Compilation can be done by
MyClass M1 (3);
MyClass m2 (' a '); Compilation does not pass
}

After the constructor is explicit, the constructor does not have the constructor of Char, because the char construction version is deleted, but in the call to Func, the compiler attempts to convert C to int, which is called by Func (\\a '), which invokes the MyClass (int) construct. smoothly through compilation. So we do not advocate explicit and delete mix. There are also types of effects for delete with normal functions.

The use of delete also includes removing the operator new operator, encoding an object that allocates the class on the heap
such as: void* operator new (std::size_t) =delete;

Synthetic copy control members may be deleted, and if a class has data members that cannot be constructed, copied, copied, or destroyed by default, the corresponding member function will be defined as deleted. So:
If a destructor for a member of a class is either deleted or inaccessible, the class's composition destructor is defined as deleted.
If the copy constructor of a member of a class is either deleted or inaccessible, the composite copy constructor for the class is also defined as deleted.
If the copy assignment operator of a member of a class is either deleted or inaccessible, or the class has a const or reference member, the composite copy assignment operator of the class is defined as deleted.
If the destructor for a member of a class is either deleted or inaccessible, or the class has a reference member, it does not have a class initializer, or the class has a const member, it does not have an in-class initializer and its type does not display the definition default constructor, the default constructor for that class is defined as the deleted

New usage of C + + default and delete

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.