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 call C++ constructor, copy constructor and overloaded equal sign operator

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

Share

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

This article mainly explains "C++ constructor, copy constructor and overload equal sign operator how to call", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C++ constructor, copy constructor and overload equal sign operator how to call it"!

Foreword:

A beginner to C++ found the following problem, where Duck is a known class and specifies the value of an object in a variety of ways:

Duck d1 (); Duck d2 (D1); Duck d3 = d1x Duck d4ten d4 = D1

The question is, how did the above D1, D2, D3, and D4 be created? Which function is called separately?

1. The difference between assignment and initialization

In C++, assignment and initialization are two different concepts:

Initialization means that the initial value of an object is specified when it is created, which can be divided into direct initialization and replication initialization (one sentence).

Assignment means that the value of an object is specified after it is created (two sentences)

For the creation of the above D1, d2, d3, and d4, respectively, it corresponds to the following situations:

Duck D1 (); / / directly initialize Duck D2 (D1); / / copy initialization Duck D3 = D1; / / copy initialization Duck d4; / / uninitialized d4 = D1; / / which function is called for assignment 2, initialization and assignment, respectively?

When initializing directly, the parameter is the value required by the object, and the constructor is called at this time; when the copy is initialized, the parameter is an existing class object, and the copy constructor is called; when assigning a value, the object is already defined. Call the "overloaded equal sign assignment operation" and use the value of another object to calculate the value of this object.

Duck D1 (); / call constructor Duck D2 (D1); / call copy constructor Duck D3 = D1; / / call copy constructor Duck d4; / / D4 = D1; / / assign

It is important to note that the following two methods are initialization, and both are called copy constructors, so it is easy to mistake the second method for assignment:

Duck D2 (D1); Duck D3 = D1 TX 3, write test classes

For the initialization and assignment of the above Duck class, in order to correctly determine what function is called in each case, the following class includes constructor, copy constructor, and overloaded equal sign assignment operator function:

Class Duck {public: Duck () {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