Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use'= default 'and' = delete'in C++

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use'= default 'and' = delete'in C++. In daily operation, I believe many people have doubts about how to use'= default 'and' = delete'in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation, hoping to help you answer the doubts about how to use'= default 'and' = delete'in C++. Next, please follow the editor to study!

Foreword:

C++ 's class has four special member functions: the default constructor, the destructor, the copy constructor, and the copy assignment operator. If the definition is not displayed during the actual coding, the compiler will generate these four types of member functions by default. Use = default and = delete to control the use of the compiler default function body.

1 = default

The = default flag has been added to Clipper 11, and when the compiler sees it, it will generate a default function definition body that is more efficient and reduce the workload of coding. Of course, a problem is introduced here: since the compiler generates constructors by default, what is the advantage of = default? Before you answer this question, take a look at this code

Class Test {public: Test (int a): X (a) {}; private: int x;}; int main () {Test test; return 0;}

As we all know, the above paragraph is compiled, because we define a constructor in the Test class, the compiler will not generate a default constructor to us if we want to solve this compilation problem, we need to provide a constructor with no parameters.

Such as:

Test () {}

After adding the above code to the class, the compiler will compile it, but imagine if the class is large and we need to initialize a lot of members in the class? At this point, the default constructor we need to provide becomes huge, wasting a lot of time initializing variables and writing a bunch of unskilled assignments or other initialization statements. Similarly, copy constructors and copy assignment functions are the same.

Default provides us with such a function, and after that, the compiler will give us the default function body to generate, reducing the workload.

The above class can be written as follows:

Class Test {public: Test (int a): X (a) {}; Test () = default;private: int x;}

Of course, = default can be added not only within class members but also outside the class, but when using = default, you must follow one rule: the default function feature can only be used for special member functions of a class or functions have no default arguments. = default

The way to write outside the class is as follows:

Class Test {public: Test (int a): X (a) {}; Test () = default;Test (const Test& ts); Test& operator = (const Test& ts); private: int x;}; Test::Test (const Test& ts) = default;Test& Test::operator = (const Test& ts) = default

The above code demonstrates the scenario in which = default is used outside of class members. But there is no destructor in the class. When coding, if the inheritance and derivation of the class are involved, especially if the derived class object is pointed to through the base class pointer, when delete is called to delete the derived object, if the base class does not display a defined destructor, the compiler will generate a destructor by default for the base class, and the base class object will be released normally, but there will also be a problem that the derived class is not released correctly. Problems such as memory leaks may occur. The correct way to solve this problem is to display and define a virtual destructor in the base class. This method was often used to solve this problem before Cure 11, but after Cure 11, we can use = default, which reduces the amount of coding we need, and the compiler generates more efficient code.

The code is as follows:

Class Base {public: virtual ~ Base () = default;private: int x;}; class A: public Base {private: int y;}; int main () {Base * pBase = new A; delete pBase; return 0;} 2 = delete

Before new 11, delete was paired with new to free up space on the heap and return resources to the operating system. After Cure 11, delete has another meaning: disable the use of member functions. To use it, add = delete after the function name.

The following code defines a class in which an integer member variable is defined. When used in the main function, two instances of the class are created, one with an integer and the other with a floating point.

The code is as follows:

Class Test {public: Test () = delete; Test (int a): X (a) {std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report