#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//前置运算符 ++i
//重载为成员函数
//T operator++()
//T operator--()
//重载全局函数
//T operator++(T)
//T operator--(T)
//调用的形式有 ++obj obj.operato4r++() operator++(obj)
//后置运算符作为二元运算符重载 i++
//多写一个参数 具体无意义
//重载为成员函数 T operator++(int) T operator--(int)
//重载为全局函数 T operator++(T, int) T operator--(T,int)
//调用形式有 obj++ obj.operator++(0) operator(obj,0)
class CDemo{
private:
int n;
public:
CDemo(int i = 0):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;
}
CDemo CDemo::operator++(int k){//后置++
CDemo tmp(*this);//保存修改前的对象 浅复制
n++;
return tmp; //返回修改前的对象
}
CDemo operator--(CDemo& d){//前置--
d.n--;
return d;
}
CDemo operator--(CDemo& d,int){//后置--
CDemo tmp(d);
d.n--;
return tmp;
}
/*
operator int(){ return n;}
int 作为一个类型强制转换运算符被重载
Demo s;
(int) s;//等效与s.int();
类型强制转换运算符重载时
不能写返回值类型
实际上其返回值类型--类型强制转换运算符代表的类型
*/
int main(int argc, char** argv) {
CDemo d(5);
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 0;
}
/*
运算符重载注意事项
1 C++不允许定义新的运算符
重载后的运算符的含义符合日常习惯
运算符重载不改变运算符优先级别
**以下运算符不能被重载: . .* :: ?: sizeof
**重载运算符() [] -> 或者赋值运算符=时,重载函数必须声明为类的成员函数。
*/
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//前置运算符 ++i
//重载为成员函数
//T operator++()
//T operator--()
//重载全局函数
//T operator++(T)
//T operator--(T)
//调用的形式有 ++obj obj.operato4r++() operator++(obj)
//后置运算符作为二元运算符重载 i++
//多写一个参数 具体无意义
//重载为成员函数 T operator++(int) T operator--(int)
//重载为全局函数 T operator++(T, int) T operator--(T,int)
//调用形式有 obj++ obj.operator++(0) operator(obj,0)
class CDemo{
private:
int n;
public:
CDemo(int i = 0):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;
}
CDemo CDemo::operator++(int k){//后置++
CDemo tmp(*this);//保存修改前的对象 浅复制
n++;
return tmp; //返回修改前的对象
}
CDemo operator--(CDemo& d){//前置--
d.n--;
return d;
}
CDemo operator--(CDemo& d,int){//后置--
CDemo tmp(d);
d.n--;
return tmp;
}
/*
operator int(){ return n;}
int 作为一个类型强制转换运算符被重载
Demo s;
(int) s;//等效与s.int();
类型强制转换运算符重载时
不能写返回值类型
实际上其返回值类型--类型强制转换运算符代表的类型
*/
int main(int argc, char** argv) {
CDemo d(5);
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 0;
}
/*
运算符重载注意事项
1 C++不允许定义新的运算符
重载后的运算符的含义符合日常习惯
运算符重载不改变运算符优先级别
**以下运算符不能被重载: . .* :: ?: sizeof
**重载运算符() [] -> 或者赋值运算符=时,重载函数必须声明为类的成员函数。
*/