【讨论】为什么要用友员来实现算符重载?# Programming - 葵花宝典
f*a
1 楼
算符重载很方便,比方说:
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
complex operator +(const complex & c);//算符重载
};
为什么有人用友员来实现算符重载, 有什么好处?
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
friend complex operator +(const complex &c1, const complex &c2);//友员
};
查了好多书都没有提到有什么好处。。
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
complex operator +(const complex & c);//算符重载
};
为什么有人用友员来实现算符重载, 有什么好处?
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
friend complex operator +(const complex &c1, const complex &c2);//友员
};
查了好多书都没有提到有什么好处。。