在C++中经常会遇到重载运算符的问题,其实运算符重载必须将运算符看做
一个函数,分清他的形参返回值,必须搞清楚内存在哪里如何分配如何回收
什么时候生成匿名对象,什么时候使用this指针返回。
运算符可以用友元函数和成员函数完成,一般来讲都使用成员函数,但是某些
特殊的情况必须使用友元函数,比如<< 因为其左操作数为ostream&类型,是不
可能进行任何修改的。
成员函数运算符重载大体步骤如下:
比如Tclass a
Tclass b
我们要重载=号
a = b
1、将需要重载的运算符看做一个函数生成operator函数名
如重载等待=
即operator=
2、分清楚形参
如果是等号重载很明显如果是成员函数形参是右操作数b原型为及
Tclass& b
这个时候第一操作数被隐藏即 *this。
3、分清返回值
为了实现如:a=b=c的级联编程,因为=连接性右到左
则为
a=(b=c)即 a.operator=(b.operator=(c))
那么b=c需要返回一个Tclass&类型,当然最好就是 b直接返回
也就是*this内存空间。
那么分析到这里我们可以写出函数原型和返回如下:
Tclass& operator=( Tclass& b)
{
...........
return *this;
}
具体实现具体分析。
下面是一个关于char*类型类的运算符重载,包括了
1、=操作符重载(深拷贝)
2、+操作符重载
3、前置++ 后置++重载
4、!= ==重载
5、<< 重载
因为涉及到char*类型的类容易引起内存泄露下面是测试程序内存泄露检查
==5613== HEAP SUMMARY:
==5613== in use at exit: 0 bytes in 0 blocks
==5613== total heap usage: 9 allocs, 9 frees, 102 bytes allocated
==5613==
==5613== All heap blocks were freed -- no leaks are possible
==5613==
==5613== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==5613== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
具体代码如下:
点击(此处)折叠或打开
-
/*************************************************************************
-
> File Name: class.cpp
-
> Author: gaopeng QQ:22389860 all right reserved
-
> Mail: gaopp_200217@163.com
-
> Created Time: Sat 25 Mar 2017 04:40:31 PM CST
-
************************************************************************/
-
-
#include<iostream>
-
#include<stdlib.h>
-
#include<string.h>
-
-
using namespace std;
-
-
-
class testop
-
{
-
private:
-
char* mystr;
-
int len;
-
public:
-
testop(const char* instr)
-
{
-
this->len = strlen(instr)+1;
-
this->mystr = new char[this->len];
-
memset(this->mystr,0,this->len);
-
strcpy(this->mystr,instr);
-
}
-
testop()
-
{
-
this->len = 0;
-
this->mystr = NULL;
-
}
-
testop(const testop& b)//copy 构造函数深拷贝
-
{
-
this->len = b.len;
-
this->mystr = new char[b.len];
-
memset(this->mystr,0,this->len);
-
strcpy(this->mystr,b.mystr);
-
}
-
void printmystr()
-
{
-
cout<<this->mystr<<endl;
-
}
-
-
~testop()
-
{
-
delete [] this->mystr;
-
}
-
//操作符重载 + 使用成员函数
-
testop operator+(const testop& b)
-
{
-
testop temp;
-
temp.len = this->len + b.len;
-
temp.mystr = new char[temp.len];
-
memset(temp.mystr,0,temp.len);
-
strcat(strcat(temp.mystr,this->mystr),b.mystr);
-
return temp;
-
}
-
//操作符重载 = 使用成员函数 深拷贝
-
testop& operator=(const testop& b)
-
{
-
if(this->mystr != NULL)//必须先释放内存
-
{
-
delete [] this->mystr;
-
}
-
this->len = b.len;
-
this->mystr = new char[this->len];
-
memset(this->mystr,0,this->len);
-
strcpy(this->mystr,b.mystr);
-
return *this;
-
}
-
//操作符重载前置(++),使用成员函数 支持链试编程
-
-
testop& operator++()
-
{
-
this->len = this->len+this->len;
-
char* temp = new char[this->len];
-
memset(temp,0,this->len);
-
strcat(strcat(temp,this->mystr),this->mystr);
-
delete [] this->mystr;
-
this->mystr = new char[this->len];
-
strcpy(this->mystr,temp);
-
delete [] temp;
-
return *this;
-
}
-
-
//操作符重载后置(++),使用成员函数 不支持链试编程 因为返回的为一个匿名对象
-
-
testop operator++(int)
-
{
-
testop tempop = *this;
-
this->len = this->len+this->len;
-
char* temp = new char[this->len];
-
memset(temp,0,this->len);
-
strcat(strcat(temp,this->mystr),this->mystr);
-
delete [] this->mystr;
-
this->mystr = new char[this->len];
-
strcpy(this->mystr,temp);
-
delete [] temp;
-
return tempop;
-
}
-
-
-
//操作符重载 << 必须使用友元函数 支持链试编程
-
friend ostream& operator<<(ostream& out,testop& b);
-
//操作符重载 == 使用成员函数
-
bool operator==(testop& b)
-
{
-
if(this->len == b.len && !strcmp(this->mystr,b.mystr))
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
//操作符重载 != 使用成员函数
-
bool operator!=(testop& b)
-
{
-
if((*this) == b )
-
{
-
return false;
-
}
-
else
-
{
-
return true;
-
}
-
}
-
-
};
-
-
ostream& operator<<(ostream& out,testop& b) // 友元函数
-
{
-
out<<b.mystr;
-
return out;
-
}
-
-
-
-
-
int main()
-
{
-
testop c("ab");
-
cout<<c<<endl;
-
c++;
-
++c;
-
cout<<"c:"<<c<<endl;
-
testop a=c;
-
cout<<"a:"<<a<<endl;
-
if(a == c)
-
{
-
cout<<"相等"<<endl;
-
}
-
a = c+a;
-
cout<<"a=c+a:"<<a<<endl;
-
if(a !=c )
-
{
-
cout<<"不相等"<<endl;
-
}
-
- }
结果如下:
gaopeng@bogon:~/cplusnew/操作符重载$ ./a.out
ab
c:abababab
a:abababab
相等
a=c+a:abababababababab
不相等
没有问题