形式
返回值类型 operator 运算符(形参表)
{
……
}
运算符重载
(1)运算符重载的实质是函数重载
(2)可以重载为普通函数,也可以重载为成员函数
class Complex
{
public:
double real,imag;
Complex( double r = 0.0, double i= 0.0 ):real(r),imag(i) { }
Complex operator-(const Complex & c);
}; Complex operator+( const Complex & a, const Complex & b)
{
return Complex( a.real + b.real, a.imag + b.imag); //返回一个临时对象
} Complex Complex::operator-(const Complex & c)
{
return Complex(real - c.real, imag - c.imag); //返回一个临时对象
} int main()
{
Complex a(,),b(,),c; //等价于c=operator+(a,b);
c = a + b;
cout << c.real << "," << c.imag << endl; //a-b等价于a.operator-(b)
cout << (a - b).real << "," << (a - b).imag << endl;
return ;
}
(3)把含运算符的表达式转换成对运算符函数的调用
(4)把运算符的操作数转换成运算符函数的参数
(5)运算符被多次重载时,根据实参的类型决定调用哪个运算符函数
(6)重载为成员函数时, 参数个数为运算符目数减一;重载为普通函数时, 参数个数为运算符目数
赋值运算符 ‘ =’重载
赋值运算符“ =”只能重载为成员函数
#include <iostream> using namespace std; class String
{
private:
char * str;
public:
String ():str(new char[]) { str[] = ;}
const char * c_str() { return str; };
String & operator = (const char * s);
String::~String( ) { delete [] str; }
}; String & String::operator = (const char * s)
{
//重载“=”以使得 obj = “hello”能够成立
delete [] str;
str = new char[strlen(s)+];
strcpy( str, s);
return * this;
} int main()
{
String s;
s = "Good Luck," ; //等价于 s.operator=("Good Luck,");
cout << s.c_str() << endl; /* 这条语句要是不注释掉就会出错,因为这是一个初始化语句而并不是赋值语句 */
// String s2 = "hello!"; s = "Shenzhou 8!"; //等价于 s.operator=("Shenzhou 8!");
cout << s.c_str() << endl;
return ;
}
浅拷贝和深拷贝
考察下面的代码
String S1, S2;
S1 = “this”;
S2 = “that”;
S1 = S2;
如果还用上面的运算符重载,那就会出现问题
(1)如不定义自己的赋值运算符,那么S1=S2实际上导致 S1.str和 S2.str指向同一地方。
(2)如果S1对象消亡,析构函数将释放S1.str指向的空间,再访问S2的时候就好玩儿了。
(3)另外,如果执行 S1 = "other";会导致S2.str指向的地方被delete
因此,要做如下修改:
String & operator = (const String & s)
{
if( this == & s)
return * this; delete [] str;
str = new char[strlen(s.str)+];
strcpy( str,s.str);
return * this;
}
ps:详细完成一个完整的String类,后续再补充
运算符重载为友元函数
一般情况下,将运算符重载为类的成员函数,是较好的选择。但有时,重载为成员函数不能满足使用要求,重载为普通函数,又不能访问类的私有成员,所以需要将运算符重载为友元。
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 c ;
c = c + 5; //有定义,相当于 c = c.operator +(5);
但是:
c = 5 + c; //编译出错
为了使得上述的表达式能成立,需要将 + 重载为普通函数。但是普通函数又不能访问私有成员,所以,需要将运算符 + 重载为友元。
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);
}; 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);
}
设计一个变长数组类
想要达到下面目的
int main()
{
CArray a; //开始里的数组是空的
/*
要用动态分配的内存来存放数组元素,需要一个指针成员变量
*/
for( int i = ;i < ;++i)
a.push_back(i); CArray a2,a3;
/*
要重载“=”
*/
a2 = a;
for( int i = ; i < a.length(); ++i )
/*
要重载“[ ]”
*/
cout << a2[i] << " " ; a2 = a3; //a2是空的
for( int i = ; i < a2.length(); ++i ) //a2.length()返回0
cout << a2[i] << " ";
cout << endl; a[] = ;
/*
要自己写复制构造函数
*/
CArray a4(a);
for( int i = ; i < a4.length(); ++i )
cout << a4[i] << " "; return ;
}
类的设计如下:
class CArray {
int size; //数组元素的个数
int *ptr; //指向动态分配的数组
public:
CArray(int s = ); //s代表数组元素的个数
CArray(CArray & a);
~CArray();
void push_back(int v); //用于在数组尾部添加一个元素v
CArray & operator=( const CArray & a);
//用于数组对象间的赋值
int length() { return size; } //返回数组元素个数
int & CArray::operator[](int i) //返回值为 int 不行!不支持 a[i] = 4
{
//用以支持根据下标访问数组元素,
// 如n = a[i] 和a[i] = 4; 这样的语句
return ptr[i];
}
}; CArray::CArray(int s):size(s)
{
if( s == )
ptr = NULL;
else
ptr = new int[s];
} CArray::CArray(CArray & a)
{
if( !a.ptr) {
ptr = NULL;
size = ;
return;
}
ptr = new int[a.size];
memcpy( ptr, a.ptr, sizeof(int ) * a.size);
size = a.size;
} CArray::~CArray()
{
if( ptr) delete [] ptr;
} CArray & CArray::operator=( const CArray & a)
{
//赋值号的作用是使“=”左边对象里存放的数组,大小和内容都和右边的对象一样
if( ptr == a.ptr) //防止a=a这样的赋值导致出错
return * this; if( a.ptr == NULL) { //如果a里面的数组是空的
if( ptr )
delete [] ptr;
ptr = NULL;
size = ;
return * this;
} if( size < a.size) { //如果原有空间够大,就不用分配新的空间
if(ptr)
delete [] ptr;
ptr = new int[a.size];
} memcpy( ptr, a.ptr, sizeof(int) * a.size);
size = a.size;
return * this;
} void CArray::push_back(int v)
{
//在数组尾部添加一个元素
if( ptr) {
int * tmpPtr = new int[size + ]; //重新分配空间
memcpy(tmpPtr, ptr, sizeof(int) * size); //拷贝原数组内容
delete [] ptr;
ptr = tmpPtr;
}
else //数组本来是空的
ptr = new int[]; ptr[size++] = v; //加入新的数组元素
}
流运算符的重载
假定c是Complex复数类的对象,现在希望写“ cout << c;”,就能以“ a+bi”的形式输出c的值,写“ cin>>c;”,就能从键盘接受“ a+bi”形式的输入,并且使得c.real = a,c.imag = b。
class Complex
{
double real,imag;
public:
Complex( double r=, double i=):real(r),imag(i){ };
friend ostream & operator<<( ostream & os, const Complex & c);
friend istream & operator>>( istream & is,Complex & c);
}; ostream & operator<<( ostream & os,const Complex & c)
{
os << c.real << "+" << c.imag << "i"; //以"a+bi"的形式输出
return os;
} istream & operator>>( istream & is,Complex & c)
{
string s;
is >> s; //将"a+bi"作为字符串读入, “a+bi” 中间不能有空格
int pos = s.find("+", );
string sTmp = s.substr(, pos); //分离出代表实部的字符串
c.real = atof(sTmp.c_str()); //atof库函数能将const char*指针指向的内容转换成 float
sTmp = s.substr(pos+, s.length()-pos-); //分离出代表虚部的字符串
c.imag = atof(sTmp.c_str());
return is;
}
类型转换运算符
类型强制转换运算符被重载时不能写返回值类型,实际上其返回值类型就是该类型强制转换运算符代表的类型
#include <iostream>
using namespace std;
class Complex
{
double real,imag;
public:
Complex(double r=,double i=):real(r),imag(i) { };
//重载强制类型转换运算符 double
operator double () { return real; } }; int main()
{
Complex c(1.2,3.4);
cout << (double)c << endl; //输出 1.2
double n = + c; //等价于 double n=2+c.operator double()
cout << n; //输出 3.2
}
自增,自减运算符的重载
1、前置运算符作为一元运算符重载
(1)重载为成员函数:
T & operator++();
T & operator--();
(2)重载为全局函数:
T1 & operator++(T2);
T1 & operator—(T2);
2、后置运算符作为二元运算符重载,多写一个没用的参数
(1)重载为成员函数:
T operator++(int);
T operator--(int);
(2)重载为全局函数:
T1 operator++(T2,int );
T1 operator—( T2,int);
class CDemo
{
private:
int n;
public:
CDemo(int i=):n(i) { }
CDemo & operator++(); //用于前置形式
CDemo operator++( int ); //用于后置形式
operator int ( ) { return n; }
friend CDemo & operator--(CDemo & );
friend CDemo operator--(CDemo & ,int);
}; CDemo & CDemo::operator++()
{
//前置 ++
n++;
return * this;
} // ++s即为: s.operator++(); CDemo CDemo::operator++( int k )
{
//后置 ++
CDemo tmp(* this); //记录修改前的对象
n++;
return tmp; //返回修改前的对象
} // s++即为: s.operator++(0); CDemo & operator--(CDemo & d)
{
//前置--
d.n--;
return d;
} //--s即为: operator--(s); CDemo operator--(CDemo & d,int)
{
//后置--
CDemo tmp(d);
d.n--;
return tmp;
} //s--即为: operator--(s, 0); int main()
{
CDemo d();
cout << (d++ ) << ","; //等价于 d.operator++(0);
cout << d << ",";
cout << (++d) << ","; //等价于 d.operator++();
cout << d << endl;
cout << (d-- ) << ","; //等价于 operator--(d,0);
cout << d << ",";
cout << (--d) << ","; //等价于 operator--(d);
cout << d << endl;
return ;
}
运算符重载的注意事项
(1)C++不允许定义新的运算符 ;
(2)重载后运算符的含义应该符合日常习惯;
complex_a + complex_b
word_a > word_b
date_b = date_a + n
(3)运算符重载不改变运算符的优先级;
(4)以下运算符不能被重载:“.” 、“.*” 、“::” 、“?:” 、 sizeof;
(5)重载运算符()、 []、 ->或者赋值运算符=时,运算符重载函数必须声明为类的成员函数。