第四章 运算符重载
C++提供了数据抽象的手段:用户自己定义数据类型 -- 类
? 调用类的成员函数—>操作它的对象
类的成员函数—>操作对象时,很不方便
? 在数学上,两个复数可以直接进行+/-等运算 Vs. 在C++中,直接将+或-用于复数是不允许的
class Complex {
public:
Complex( double r = 0.0, double i= 0.0 ){
real = r;
imaginary = i;
}
double real; // real part
double imaginary; // imaginary part
};
Complex operator+ (const Complex & a, const Complex & b)
{
return Complex( a.real+b.real, a.imaginary+b.imaginary);
} // “类名(参数表)” 就代表一个对象
Complex a(1,2), b(2,3), c;
c = a + b;// 相当于什么? operator+(a,b)
重载为普通函数时,参数个数为运算符目数class Complex {
public:
Complex( double r= 0.0, double m = 0.0 ):real(r), imaginary(m) { } // constructor
Complex operator+ ( const Complex & ); // addition
Complex operator- ( const Complex & ); // subtraction
private:
double real; // real part
double imaginary; // imaginary part
};
// Overloaded addition operator
Complex Complex::operator+(const Complex & operand2) {
return Complex( real + operand2.real,imaginary + operand2.imaginary );
}
// Overloaded subtraction operator
Complex Complex::operator- (const Complex & operand2){
return Complex( real - operand2.real,imaginary - operand2.imaginary );
}
int main(){
Complex x, y(4.3, 8.2), z(3.3, 1.1);
x = y + z;// 相当于什么? y.operator+(z)
x = y - z;// 相当于什么? y.operator-(z)
return 0;
}
重载为成员函数时,参数个数为运算符目数减一需要 重载赋值运算符 ‘=’
赋值运算符 “=” 只能重载为 成员函数编写一个长度可变的字符串类String
? 包含一个char * 类型的成员变量
—> 指向动态分配的存储空间
? 该存储空间用于存放 ‘\0’ 结尾的字符串
class String {
private:
char * str;
public:
String () : str(NULL) { } //构造函数, 初始化str为NULL
const char * c_str() { return str; } //返回值为const类型,保证str不会被修改。比如char* p=str.c_str();则编译器会报错,类型不匹配。
char * operator = (const char * s);
~String( );//需要考虑String对象是否指向了动态分配的存储空间
};
//重载‘=’使得obj = “hello”能够成立
char * String::operator = (const char * s){
if(str) delete [] str;
if(s) { //s不为NULL才会执行拷贝
str = new char[strlen(s)+1];
strcpy(str, s);
}
else
str = NULL;
return str;
}
String::~String( ) {
if(str) delete [] str;
};
int main(){
String s;
s = “Good Luck,” ;
cout << s.c_str() << endl;
// String s2 = “hello!”; //这条语句要是不注释掉就会出错
s = "Shenzhou 8!";
cout << s.c_str() << endl;
return 0;
}
S1 = S2;
在 class MyString 里添加成员函数:
String & operator = (const String & s) {
if(str) delete [] str;
str = new char[strlen(s.str)+1];
strcpy(str, s.str);
return * this;
}
考虑下面语句,是否会有问题?
MyString s; s = “Hello”; s = s;
正确写法:
String & String::operator = (const String & s){
if(str == s.str) return * this;//增加此行
if(str) delete [] str;
if(s.str) { //s.str不为NULL才会执行拷贝
str = new char[strlen(s.str)+1];
strcpy( str,s.str);
}
else
str = NULL;
return * this;
}
为 String类编写 复制构造函数 时,会面临和 ‘=’ 同样的问题,用同样的方法处理
String::String(String & s)
{
if(s.str) {
str = new char[strlen(s.str)+1];
strcpy(str, s.str);
}
else
str = NULL;
}
class Complex{
double real, imag;
public:
Complex(double r, double i):real(r), imag(i){ };
Complex operator+(double r);
};
Complex Complex::operator+(double r){ //能解释 c+5
return Complex(real + r, imag);
}经过上述重载后:Complex operator+ (double r, const Complex & c) {
//能解释 5+c
return Complex( c.real + r, c.imag);
}普通函数不能访问私有成员 —> 将运算符+重载为友元函数class Complex {
double real, imag;
public:
Complex( double r, double i):real(r),imag(i){ };
Complex operator+( double r );
friend Complex operator + (double r, const Complex & c);
};原文:http://blog.csdn.net/buxizhizhou530/article/details/46011627