Don';不需要为+;超载? 我在C++上重载+运算符,像这样: #include <iostream> class Cent { private: int m_nCent; public: Cent(){ }; Cent(int n); int getCent() const; void setCent(int); friend Cent operator+(const Cent &c1, const Cent &c2); friend Cent operator+(const Cent &c1, const int); }; Cent::Cent(int n) { setCent(n); } int Cent::getCent() const { return Cent::m_nCent; } void Cent::setCent(int n) { Cent::m_nCent = n; } Cent operator+(const Cent &c1, const Cent &c2) { return Cent(c1.getCent() + c2.getCent()); } Cent operator+(const Cent &c1, const int n) { return Cent(c1.getCent() + n); } int main() { Cent c1(5); Cent c2(4); Cent sum; sum = c1 + c2; std::cout << sum.getCent() << std::endl; sum = c1 + 7; std::cout << sum.getCent() << std::endl; sum = 9 + c1; std::cout << sum.getCent() << std::endl; return 0; } #包括 班分 { 私人: 国际货币基金组织; 公众: 分({}); 分(整数n); int getCent()常量; 无效设置(int); 友元分运算符+(常数分和c1、常数分和c2); 友元分运算符+(常数分&c1,常数整数); }; 分:分(整数n) { 塞森特(n); } int Cent::getCent()常量 { 返回分::m\n分; } void Cent::setCent(int n) { Cent::m_nCent=n; } 分操作员+(常数分和c1、常数分和c2) { 返回分(c1.getCent()+c2.getCent()); } 分运算符+(常数分和c1,常数整数n) { 返回分(c1.getCent()+n); } int main() { 分c1(5); 分c2(4); 分金额; 总和=c1+c2; std::cout

Don';不需要为+;超载? 我在C++上重载+运算符,像这样: #include <iostream> class Cent { private: int m_nCent; public: Cent(){ }; Cent(int n); int getCent() const; void setCent(int); friend Cent operator+(const Cent &c1, const Cent &c2); friend Cent operator+(const Cent &c1, const int); }; Cent::Cent(int n) { setCent(n); } int Cent::getCent() const { return Cent::m_nCent; } void Cent::setCent(int n) { Cent::m_nCent = n; } Cent operator+(const Cent &c1, const Cent &c2) { return Cent(c1.getCent() + c2.getCent()); } Cent operator+(const Cent &c1, const int n) { return Cent(c1.getCent() + n); } int main() { Cent c1(5); Cent c2(4); Cent sum; sum = c1 + c2; std::cout << sum.getCent() << std::endl; sum = c1 + 7; std::cout << sum.getCent() << std::endl; sum = 9 + c1; std::cout << sum.getCent() << std::endl; return 0; } #包括 班分 { 私人: 国际货币基金组织; 公众: 分({}); 分(整数n); int getCent()常量; 无效设置(int); 友元分运算符+(常数分和c1、常数分和c2); 友元分运算符+(常数分&c1,常数整数); }; 分:分(整数n) { 塞森特(n); } int Cent::getCent()常量 { 返回分::m\n分; } void Cent::setCent(int n) { Cent::m_nCent=n; } 分操作员+(常数分和c1、常数分和c2) { 返回分(c1.getCent()+c2.getCent()); } 分运算符+(常数分和c1,常数整数n) { 返回分(c1.getCent()+n); } int main() { 分c1(5); 分c2(4); 分金额; 总和=c1+c2; std::cout,c++,operator-overloading,C++,Operator Overloading,您有一个隐式转换构造函数Cent(int)。9+c1调用将扩展到Cent(9)+c1,并调用Cent,Cent重载。您有一个隐式转换构造函数Cent(int)。9+c1调用将扩展到Cent(9)+c1并调用Cent,Cent重载。您的类具有转换构造函数 Cent(int n); 它允许将int类型的对象隐式转换为Cent类型的临时对象 例如,如果您将构造函数声明为 explicit Cent(int n); 然后,将不会编译带有运算符调用的代码 或者,如果您要按以下方式声明运算符,请删除第一

您有一个隐式转换构造函数
Cent(int)
9+c1
调用将扩展到
Cent(9)+c1
,并调用
Cent,Cent
重载。

您有一个隐式转换构造函数
Cent(int)
9+c1
调用将扩展到
Cent(9)+c1
并调用
Cent,Cent
重载。

您的类具有转换构造函数

Cent(int n);
它允许将
int
类型的对象隐式转换为
Cent
类型的临时对象

例如,如果您将构造函数声明为

explicit Cent(int n);
然后,将不会编译带有运算符调用的代码

或者,如果您要按以下方式声明运算符,请删除第一个参数的限定符
const

friend Cent operator+( Cent &c1, const Cent &c2);

然后代码也不会被编译。

您的类具有转换构造函数

Cent(int n);
它允许将
int
类型的对象隐式转换为
Cent
类型的临时对象

例如,如果您将构造函数声明为

explicit Cent(int n);
然后,将不会编译带有运算符调用的代码

或者,如果您要按以下方式声明运算符,请删除第一个参数的限定符
const

friend Cent operator+( Cent &c1, const Cent &c2);

然后代码也不会被编译。

换句话说,您只需要一个重载,
Cent操作符+(const Cent&c1,const Cent&c2);
将构造函数标记为
显式Cent(int n);
,则您将无法隐式转换,并将在
9+c1
行中得到一个错误。换句话说,您只需要一个重载,
Cent运算符+(const Cent&c1,const Cent&c2);
将构造函数标记为
显式Cent(int n);
,则您将无法隐式转换,并且将在
9+c1
行中得到一个错误。