C + + operator= considerations

Source: Internet
Author: User

Let's take a look at the following code:

#include <iostream>using namespace Std;class point{public:point (float x=0.0,float y=0.0): _x (x), _y (y) {}point & operator= (const point& RHS), void Printdata () {cout<< "_x=" <<_x<<endl;cout<< "_y=" <<_y<<endl;} Protected:float _x,_y;}; Inline point& point::operator= (const point& p) {cout<< "point::operator=" <<endl;_x=p._x;_y=p._y; return *this;} Class Point3d:public virtual Point{public:point3d (float x=0.0,float y=0.0,float z=0.0):P oint (x, y), _z (z) {}point3d & operator= (const point3d& p) {if (this==&p) return *this;cout<< "point3d::operator=" <<endl;_z =p._z;return *this;} void Printdata () {point::p rintdata ();cout<< "_z=" <<_Z<<ENDL;} Protected:float _z;}; int main () {Point3D A (n/a), B (4,5,6); A=b;a.printdata (); return 0;}

The program output results are as follows:


Parsing: A derived class defines an assignment operator, but does not implicitly call the assignment operator of the base class, which differs from the constructor and copy constructors.

Take a look at the following code:

#include <iostream>using namespace Std;class point{public:point (float x=0.0,float y=0.0): _x (x), _y (y) {}point & operator= (const point& RHS), void Printdata () {cout<< "_x=" <<_x<<endl;cout<< "_y=" <<_y<<endl;} Protected:float _x,_y;}; Inline point& point::operator= (const point& p) {cout<< "point::operator=" <<endl;_x=p._x;_y=p._y; return *this;} Class Point3d:public virtual Point{public:point3d (float x=0.0,float y=0.0,float z=0.0):P oint (x, y), _z (z) {}void Printdata () {point::p rintdata ();cout<< "_z=" <<_Z<<ENDL;} Protected:float _z;}; int main () {Point3D A (n/a), B (4,5,6); A=b;a.printdata (); return 0;}

The program output results are as follows:


Analysis: The derived class does not define its own assignment operator, and the compiler synthesizes an assignment operator function for the derived class (the synthetic assignment operator function, which inserts the code that calls the base class assignment operator function), and the synthetic assignment operator function implicitly calls the assignment operator function of the base class.


Let's look at the last piece of code:

#include <iostream>using namespace Std;class point{public:point (float x=0.0,float y=0.0): _x (x), _y (y) {}point & operator= (const point& RHS), void Printdata () {cout<< "_x=" <<_x<<endl;cout<< "_y=" <<_y<<endl;} Protected:float _x,_y;}; Inline point& point::operator= (const point& p) {cout<< "point::operator=" <<endl;_x=p._x;_y=p._y; return *this;} Class Point3d:public virtual Point{public:point3d (float x=0.0,float y=0.0,float z=0.0):P oint (x, y), _z (z) {}point3d & operator= (const point3d& p) {if (this==&p) return *this; Point::operator= (p);cout<< "point3d::operator=" <<endl;_z=p._z;return *this;} void Printdata () {point::p rintdata ();cout<< "_z=" <<_Z<<ENDL;} Protected:float _z;}; int main () {Point3D A (n/a), B (4,5,6); A=b;a.printdata (); return 0;}

The program output results are as follows:


Analysis: The assignment operator function defined by the derived class shows the assignment operator function that calls the base class definition, first assigns a value to the base class object part, and then assigns a value to the derived class object.

C + + operator= considerations

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.