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 realize operator overloading in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to achieve operator overloading in C++". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "how to achieve operator overloading in C++".

1. Citing examples: class Complex {private: double Real,Image;public: Complex (): Real (0), Image (0) {} Complex (double r, double I): Real (r), Image (I) {} ~ Complex () {}; int main () {Complex C1 (1.2); Complex c2 (45Yu56); Complex c3; c3 = c1.Add (c2);}

Class is very simple. Next we will discuss how to write an Add function so that the properties of the two objects are added together and a new object is returned.

The first kind:

Complex::Complex Add (const Complex & c) {Complex co; co.Real = this- > Real + c.Real; co.Image = this- > Image + c.image; return co;}

Question1: how to write the most space-saving Add function?

The second kind:

Complex::Complex Add (const Complex & c) const {return Complex (c.Real + this- > Real, c.Image + this.Image);}

Since there is no need to change the property value of the calling object, we add const decorations to the this pointer.

The analysis process is as follows:

Question2: why doesn't the first way save space?

First of all, the first kind of code is cumbersome, and another object is created in the function stack frame (line 3). And the function type is a value-passing type (line 6), which returns a dead-value object. Then the whole Add function space will produce two objects, resulting in a waste of space.

The second code creates an unnamed object, which reduces the creation of a co object and passes the unnamed object directly to c3 in the main function as a dead value object.

Question 3: can we change the Add function to a reference type to reduce the creation of dead-value objects

Complex::Complex & Add (const Complex & c) const {return Complex (c.Real + this- > Real, c.Image + this.Image);}

VS2019 found an error and cannot return a reference object:

We analyze:

Question 4: can we change the name of this Add function to the + operator?

/ / Complex::Complex Add (constComplex & c) constComplex::Complex + (constComplex & c) const {Complex co; co.Real = this- > Real + c.Real; co.Image = this- > Image + c.Image; return co;} int main () {. / / c3 = c1.Add (c2); c3 = c1.+ (c2); / / change the place of the original Add to the plus sign. ...}

If used in this way, the compiler will report an error, and the operator cannot be used as a valid function name.

Question 5: how do I use the + budget character as a function name?

This leads to the key point of today, function operator overloading.

In C++, to make the operator a valid function name, we add an operator before the operator.

Complex operator+ (const Complex & c) const {return Complex (c.Real + this- > Real,c.Image + this- > Image);} int main () {Complex C1 (1.2Power2.3); Complex c2 (10Power10); Complex c3; c3 = C1 + c2; / / the above line is actually / / c3 = c1.operator+ (c2); / / c3 = operator+ (& C1 catalog c2); / / the compiler will also go through a compilation}

Previous blogs have analyzed the origin of line 15, passing the address of C1 to the this pointer and c2 as an alias for formal parameter c to the function.

2. Functions automatically established in the class

In C++, if we define a class, it automatically creates six default functions for us:

Constructor destructor copy constructor assignment function ordinary object & (take address symbol) overload constant object & (take address character) overload

The code example is as follows:

Class Object {public: Object () {} / / Constructor ~ Object () {} / / Destructor Object (const Object & obj) {} / / copy constructor Object & operator= () {const Object & obj} / / assignment function {return * this } Object * operator& () / / overload of ordinary object & (take address character) {return this;} const Object * operator& () const / / regular object & (take address character) overload {return this;}}

Then, under the C11 standard, two more default functions have been added, which will not be delved into here:

Mobile constructor mobile assignment function

3. Parsing of overloaded assignment operator

Go back to the original example:

Class Object {int value;public: Object () {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