= Overloaded in C ++ class, copy assignment function, an example of overloading. It must be within the class

#include <the iostream> 

// Overloading "operator =" Inside class 
// = is a unary operator. Do not write the compiler will provide a default copy assignment function. You can disable the default by explicitly "= delete". For the default of a complex class = may cause problems, please pay special attention. 

////////////////////////////////////////////////// //////// 

class the Rectangle 
{ 
public: 
	the Rectangle (int W, int H) 
		: width (W), height (H) 
	{}; 

	~ the Rectangle () {}; 

	BOOL operator == (the Rectangle & REC); 

	& operator = the Rectangle (the Rectangle & REC); 


public: 
	int width; 
	int height; 
}; 

//////////////////////////////// ////////////////////////// 
BOOL 
of the Rectangle :: operator == (of the Rectangle & REC) // same class object mutual friend, so access to private objects. == is a binary operator, hidden within this class 
{
	this- return> == rec.height height 
		&& this-> == rec.width width; 
} 

the Rectangle & 
the Rectangle :: operator = (REC the Rectangle &) 
{ 
	// check must be self-replicating in the =! We must first define the == method. 
	// avoid unnecessary overhead, as well as to avoid influencing the use of certain functions of both variables. 

	IF (the this == * REC) 
		return * the this; 

	this-> height = rec.height; 
	this-> width = rec.width; 

	return * the this; 

} 

/////////////// /////////////////////////////////////////// 

int main () 
{ 
	the Rectangle A (40, 10); 
	the Rectangle A = B; 

	STD :: << COUT (A == B) STD :: << endl; 

	return 0; 
}

  

Guess you like

Origin www.cnblogs.com/alexYuin/p/11965172.html