1.友元
为了结果运算符重载过程中,自定义类型在运算符右侧的问题
是一种非成员函数就可以访问类内私有成员的函数
2. 单成员和双成员
class base{
private:
int val;
public:
base operator+(int a)const;
friend base operator (int a, const base &t)const;
}
base base::operator+(int a)const{
base temp;
temp.val = a+val;
return base;
}
base operator+(int a,const base & t)const{
//base temp;
//temp.val = a+val;
//return base;
return t+a;
}
class base{
private:
int val;
public:
base operator+(const base & t)const;
//friend base operator+ (const base &t,const base &b);
}
base base::operator+(const base &t)const{
base temp;
temp.val = val + t.val;
return temp;
}
base operator+(const base &t,const base& b){
base temp;
temp.val = b.val + t.val;
return temp;
}
3. 运算符重载函数也可以发生重载
定义的运算符的一元和二元版本
4.转换函数
假如base 中有 base(double a);这样的构造函数,
,则可以编写以下代码:
base b1;
b1 = 10.5;
程序将使用构造函数base (double a) 来创建一个临时的base对象,并将10.5作为初始化值。随后采用逐成员赋值方式将临时对象的内容复制到b1中。
5. explicit
使用explicit 可以关闭4中的自动属性