c++ 操作符重载

来源:互联网 发布:360数据恢复好用不 编辑:程序博客网 时间:2024/06/16 18:57

需要注意的是 ,像+ ,- ,* 等二元操作符以及<< ,>> 一般是要使用友元函数,这是由于友元函数的特点是不属于对象,不含this指针,可以访问类的私有函数。 

像=,+=,-=,*= 等一元操作符 一般是返回*this 的引用,降低开销,又方便连续使用,如 a = b = c = d ;等。

源码可以直接运行,已经比较清晰了。

#include <iostream>

using namespace std;
class A
{
public:
A(double data = 0.0):m_data(data){}
A& operator = (const A& rhs)
{
m_data = rhs.m_data;
return *this;
}

friend A operator + (const A& lhs,const A& rhs);
friend A operator - (const A& lhs,const A& rhs);
friend A operator * (const A& lhs,const A& rhs);
friend A operator + (const A& lhs,double rhs);
friend A operator + (double lhs,const A& rhs);
friend A operator * (const A& lhs,double rhs);
friend A operator * (double lhs,const A& rhs);
friend A operator - (const A& lhs,double rhs);
friend A operator - (double lhs,const A& rhs);

friend ostream& operator << (ostream& out,const A& a);
friend istream& operator >> (istream& in,A& d);


A& operator += (const A& rhs);
A& operator -= (const A& rhs);
A& operator *= (const A& rhs);  
private:
double m_data;
};


A& A::operator+=( const A& rhs )
{
m_data += rhs.m_data;
return *this;
}


A& A::operator-=( const A& rhs )
{
m_data -= rhs.m_data;
return *this;
}


A& A::operator*=( const A& rhs )
{
m_data *= rhs.m_data;
return *this;
}


A operator + (const A& lhs,const A& rhs)
{
A res(0);
res.m_data = lhs.m_data + rhs.m_data;
return res;
}
A operator + (const A& lhs,double rhs)
{
A res(0);
res.m_data = lhs.m_data + rhs;
return res;
}


A operator + (double lhs,const A& rhs)
{
A res(0);
res.m_data = lhs + rhs.m_data;
return res;
}
A operator - (const A& lhs,const A& rhs)
{
A res(0);
res.m_data = lhs.m_data - rhs.m_data;
return res;
}
A operator - (const A& lhs,double rhs)
{
A res(0);
res.m_data = lhs.m_data - rhs;
return res; 
}
A operator - (double lhs,const A& rhs)
{
A res(0);
res.m_data = lhs - rhs.m_data;
return res; 
}
A operator * (const A& lhs,const A& rhs)
{
A res(0);
res.m_data = lhs.m_data * rhs.m_data;
return res;
}


A operator * (const A& lhs,double rhs)
{
A res(0);
res.m_data = lhs.m_data * rhs;
return res;
}
A operator * (double lhs,const A& rhs)
{
A res(0);
res.m_data = lhs * rhs.m_data;
return res;
}




ostream& operator << (ostream& out,const A& a)
{
out << a.m_data;
return out;
}


istream& operator >> (istream& in,A& d)
{
in >> d.m_data;
return in;
}


int main(int argc, char* argv[])
{
A a(2.3);
A b(1.2);
A d(3.4);
A c;
A zero;
cout << "a" << a << endl;
cout << "b" << b << endl;
cout << "d" << d << endl;
cout << "c" << c << endl;


c = a + b + d;
cout << "c = a + b + d ,c = " <<  c << endl;
c= a + b;
cout <<"c = a + b ,c = "<< c << endl;
c= a + 1.0;
cout << "c = a + 1.0 ,c = " << c << endl;
c= a - b;
cout << "c = a - b ,c = " << c << endl;
c= a - 1.0;
cout << "c = a - 1.0 ,c = " << c << endl;
c= a * b;
cout << "c = a * b ,c = " << c << endl;
c= a * 1.0;
cout << "c = a * 1.0 ,c = " << c << endl;
c = zero;
c += a;
cout << "c += a ,c = " << c << endl;
c = zero;
c -= a;
cout << "c -= a ,c = " << c << endl;
c = zero;
c *= a;
cout << "c *= a ,c = " << c << endl;
cin >> c;
cout << "c = " << c <<endl;
system("pause");
return 0;
}


有些较特殊的操作符重载如

    inline operator T* () const { return m_pData; }//m_pData;为私有

 T*  必须写在operator 和 () z之间

使用时如

DataList<char> l;

char *str = l;


像这样就可以直接获取到对象的私有成员(改成保护成员也可以)。

程序使用如下

#include <iostream>
using namespace std;


template <typename T>
class DataList
{
public:
#define INIT_DATALIST 10
DataList():m_count(0),m_maxCount(INIT_DATALIST)
{
m_pData = (T*)malloc(sizeof(T) * m_maxCount);
}
inline void add(T t)
{
if (m_count > m_maxCount)
{
m_maxCount = max(m_count,m_maxCount + INIT_DATALIST);
m_pData = (T *)realloc(m_pData,sizeof(T) * m_maxCount);
}
memcpy(&m_pData[m_count],&t,sizeof(t)); 
m_count++;
}
inline operator T* () const { return m_pData; }
private:
T* m_pData;
int m_count;
int m_maxCount;
};

int main(int argc,char **argv)
{
DataList<int> l;
for (int i = 0;i < 10;i++)
{
l.add(i);
}
int *lst = l;
for (int i = 0;i < 10;i++)
{
printf("%d\n",lst[i]);
}
system("pause");
}

另外的是操作符重载是不可以只靠返回类型来判断重载的是哪个函数的,这是c++重载函数的特性。


原创粉丝点击