关于运算符重载问题
来源:百度知道 编辑:UC知道 时间:2024/06/17 21:05:10
关于运算符重载问题
class CINT
{
private:
int n;
public:
CINT(int m=0){n=m;}
int GETN () const { return n:}
CINT operator + (const CINT &rhs);
friend CINT operator *(const CINT& ihs,const CINT& rhs);
friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
};
能帮我解释一下类中的
CINT operator + (const CINT &rhs);
friend CINT operator *(const CINT& ihs,const CINT& rhs);
friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
各是什么意思吗?
尤其是friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
这句,只知道是友元,但不知道具体是什么意思
希望好心人能帮我解释一下,感激你们。
class CINT
{
private:
int n;
public:
CINT(int m=0){n=m;}
int GETN () const { return n:}
CINT operator + (const CINT &rhs);
friend CINT operator *(const CINT& ihs,const CINT& rhs);
friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
};
能帮我解释一下类中的
CINT operator + (const CINT &rhs);
friend CINT operator *(const CINT& ihs,const CINT& rhs);
friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
各是什么意思吗?
尤其是friend std::ostream& operator <<(std::ostream& os,const CINT& rhs);
这句,只知道是友元,但不知道具体是什么意思
希望好心人能帮我解释一下,感激你们。
第一个是重载+运算符,重载为成员函数,参数是一个引用,+就相当于CINT的一个函数一样,如一个CINT对象a,调用a+b时就相当于把b当成+的参数。
第二个是重载*为友元函数,这是调用a*b时,a、b都作为*的参数,返回值是乘积结果。
第三个是对输出流运算符<<的重载,其实我们输出使用的cout是ostream类的一个对象,调用cout<<a,cout和a都作为<<的参数。