C++98,C++0x,C++11对“哪些运算符重载能够重载”有一致的规定。详细例如以下:

class Cperson
{
private:
int data;
public:
Cperson operator+(Cperson const& other)const;//类成员函数实现
friend Cperson operator*(Cperson const& p1,Cperson const& p2);//友元函数方式实现
Cperson& operator+=(Cperson const& other);
};
Cperson Cperson::operator+(Cperson const& other)const
{
Cperson temp;
temp.data=this->data+other.data;
return temp;
}
Cperson operator*(Cperson const& p1,Cperson const& p2)
{
Cperson temp;
temp.data=p1.data*p2.data;
return temp;
}
Cperson& Cperson::operator+=(Cperson const& other)
{
Cperson temp=*this=other;//因为前面已经实现了+运算符,此处直接使用
return *this=temp;
}
class Cperson
{
private:
char* name;
public:
Cperson& operator=(Cperson const& other);
};
Cperson& Cperson::operator=(Cperson const& other)
{
if(this->name)//先释放原先的数据
delete[] name;
if(other)//如果传入的参数不为空
{
int len=strlen(other.name);
this->name=new char[len+1];
strcpy(this->name,other.name);
}
else//如果传入的参数为空
{
other.name=nullptr;
}
};class A;
A a;
A b = a; // 调用拷贝构造函数, 因为b是第一次初始化
A c(a); // 调用拷贝构造函数, 因为c是第一次初始化
b = c; // 调用赋值运算符, 因为b已经初始化过了class Cperson
{
private:
int data;
public:
Cperson& operator++();//前置++,返回值为引用
Cperson operator++(int);//后置++,返回值不为引用
};
Cperson& Cperson::operator++()
{
data++;
return *this;
}
Cperson Cperson::operator++(int)
{
Cperson temp=*this;
data++;
return temp;
}设计规则:
class Cperson
{
private:
int id;
int age;
public:
Cperson(int Id, int Age) :id(Id), age(Age) {}
int getId()const;
int getAge()const;
friend bool operator==(const Cperson &p1, const Cperson &p2)
{
return ((p1.getId() == p2.getId()) && (p1.getAge() == p2.getAge()));
}
friend bool operator!=(const Cperson &p1, const Cperson &p2)
{
return !(p1 == p2);
}
};
int Cperson::getId()const { return id; }
int Cperson::getAge()const { return age; }参数:
返回值:返回它的ostream形参
参数:
返回值:某个给定流的引用
输入时可能产生的错误:
class Cpoint
{
private:
int x;
int y;
public:
friend ostream& operator<<(ostream& os, const CMyPoint const& pt);//输出流
friend istream& operator>>(ostream& is, const CMyPoint const& pt);//输入流
}
ostream& operator<<(ostream& os, const CMyPoint const& pt)
{
os<<pt.x<<pt.y;
return os;
}
istream& operator>>(ostream& is,const CMyPoint const& pt)
{
is>>pt.x>>pt.y;
return is;
}下标操作符 [] 通常用于访问数组元素。重载该运算符用于增强操作 C++ 数组的功能。
下面的实例演示了如何重载下标运算符 []。
#include <iostream>
using namespace std;
const int SIZE = 10;
class Safearray
{
private:
int arr[SIZE];
public:
Safearray()
{
register int i;
for(i = 0; i < SIZE; i++)
{
arr[i] = i;
}
}
int& operator[](int i)
{
if( i > SIZE )
{
cout << "索引超过最大值" <<endl;
// 返回第一个元素
return arr[0];
}
return arr[i];
}
};
int main()
{
Safearray A;
cout << "A[2] 的值为 : " << A[2] <<endl;
cout << "A[5] 的值为 : " << A[5]<<endl;
cout << "A[12] 的值为 : " << A[12]<<endl;
return 0;
}类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。
运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。
间接引用运算符 -> 可被定义为一个一元后缀运算符。也就是说,给出一个类:
classPtr{
//...
X *operator->();};类 Ptr 的对象可用于访问类 X 的成员,使用方式与指针的用法十分相似。例如:
void f(Ptr p ){
p->m =10;// (p.operator->())->m = 10}语句 p->m 被解释为 (p.operator->())->m。同样地,下面的实例演示了如何重载类成员访问运算符 ->。
#include <iostream>
#include <vector>
using namespace std;
// 假设一个实际的类
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};
// 静态成员定义
int Obj::i = 10;
int Obj::j = 12;
// 为上面的类实现一个容器
class ObjContainer {
vector<Obj*> a;
public:
void add(Obj* obj)
{
a.push_back(obj); // 调用向量的标准方法
}
friend class SmartPointer;
};
// 实现智能指针,用于访问类 Obj 的成员
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc)
{
oc = objc;
index = 0;
}
// 返回值表示列表结束
bool operator++() // 前缀版本
{
if(index >= oc.a.size() - 1) return false;
if(oc.a[++index] == 0) return false;
return true;
}
bool operator++(int) // 后缀版本
{
return operator++();
}
// 重载运算符 ->
Obj* operator->() const
{
if(!oc.a[index])
{
cout << "Zero value";
return (Obj*)0;
}
return oc.a[index];
}
};
int main() {
const int sz = 10;
Obj o[sz];
ObjContainer oc;
for(int i = 0; i < sz; i++)
{
oc.add(&o[i]);
}
SmartPointer sp(oc); // 创建一个迭代器
do {
sp->f(); // 智能指针调用
sp->g();
} while(sp++);
return 0;
}struct absInt {
bool operator()(int value) {
return value < 0 ? true : false;
}
};
int main()
{
absInt a;
printf("%d\n", a(-1));//打印1
return 0;
}