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