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

What are the hidden default functions in the C++ class

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the C++ class hidden default functions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Six default functions hidden in the Test class

Class Test {public: / / default constructor Test (); / destructor ~ Test (); / / copy constructor Test (const Test& t); / / assignment function Test& operator= (const Test& x); / / General object address function Test* operator& () / / constant object takes address function const Test* operator& () const Private: int data; / / int * data / / Note: if the member contains a pointer type, you need to overload the copy function and assignment function / / otherwise it will cause a shallow copy / / in addition, you need to pay attention to releasing the additional resources used in the class (resources requested by the heap area)} in the destructor. 1. Constructor function

Function: initialize the memory space where the object is located and assign resources to the object

Features:

1. Can be overloaded: it can be overloaded by default and multi-parameter according to the actual need.

two。 Object independent: the object cannot call the constructor and can only be called at the object definition point

/ / member function implementation outside the class, you need to specify the scope before the function name, otherwise the compiler will think that the default constructor {} / / in defining a common function Test::Test () / / class. In addition, the constructor can support overloading, we can write some constructors ourselves as needed / / note that if we write the constructor ourselves Then the compiler will not provide the default constructor Test::Test (int d = 0) / / default constructor {data = d } Test::Test (int d = 0): data (d) / / default constructor to initialize {} with an initialization list

The difference between initialization and initialization is that the initialization list is true initialization, which tells the compiler how to assign values to members when instantiating objects, while the assignment rules for the former are written inside the constructor and are performed after member variables have been generated.

Initialization column example:

Tips: note the difference between list parameter initialization and list initialization. List parameter initialization is initialized by fun (int ia): mval (ia) colon + parentheses after the formal parameter list of the function, while list initialization generally refers to such as std::vector vec {1Power2, 3, 4, 5}; vec {1, 2, 3, 4, 5}; this kind of behavior, initializing the "array" through the list enclosed by {}. In fact, there is also a concept of in-place initialization in the Category 11 standard, which is not discussed here.

There are a few features to note about the initialization list:

For example, the following operations: member variables have reference types and const types. In C++, the const type is specified as a constant, which must be initialized when it is defined, while referencing an alias that we think is a variable also needs to be initialized when defined. So the following actions can only be initialized using the initialization list.

Class Test {public: / * error constant, reference initializes Test (int a, int bdirection int c) {ma = a; mb = a; / / error mc = a when defined. / / error} * / Test (int a, int b, int c): ma (a), mb (b), mc (c) {} private: int ma; int& mb; const int mc;}

In addition, if there are multiple member variables that need to be initialized using the initialization list, you need to pay attention to one detail. The order of initialization is only related to the order in which the member variables are defined.

For example, the following program can be written as Test (int a): ma (mb), mb (a) {} or Test (int a): mb (a), ma (mb) {} because the order in which member variables are defined is int mb; int ma;, that is, the order of assignment is independent of the initialization list, but only related to the order in which member variables are defined.

Class Test {public: Test (int a): ma (mb), mb (a) / / mb is defined, first assigns a value to mb, and then assigns a value to ma {} / * the following wrong way of writing: explanation: 1. Mb is defined first, ma is then defined. The order of initialization using the parameter list is first mb, then ma 2. Both ma and mb are random values before initialization, or are populated with 0xcccccccc (see compiler implementation) 3. When initializing, mb (ma), mb is initialized to an invalid value (random value or 0xcccccccc) ma (a) and ma is initialized to the value of a. Therefore, if Test (10) is called, then mb:-858993460 ma: 10 * / Test (int a): mb (ma), ma (a) {} public: void Show () {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