我正在研究实现一元“否定”、“符号反转”或“减法”运算符,可能作为我班级的朋友函数。
我对正确方法的猜测是:
namespace LOTS_OF_MONNIES_OH_YEAH { // sorry, couldn’t resist using this namespace name
class cents
{
public:
cents(const int _init_cents)
: m_cents(_init_cents)
{
}
public:
friend inline cents operator-(const cents& _cents);
private:
int m_cents;
};
inline cents operator-(const cents& _cents)
{
return cents(-(_cents.m_cents));
}
}
我的猜测正确吗?
PS:理想情况下,命名空间名称应该小写,因为大写通常专门用于常量,但是我认为大写提供了更大的影响。
PPS:从这里撕掉示例