小编CSM*_*CSM的帖子

二进制运算符声明的'static'关键字

在C ++中,当LHS是要声明的类时,二进制运算符可以被一个或两个运算符覆盖。如果使用两个参数声明,则必须是非成员函数。在此代码中,两个声明是相同的。

class MyClass
{
     public:
         MyClass operator+(const MyClass&);
}
MyClass operator+(const MyClass&, const MyClass&);
Run Code Online (Sandbox Code Playgroud)

是否有理由不能将后者作为静态成员函数完成?像这样

class MyClass
{
     public:
         static MyClass operator+(const MyClass&, const MyClass&);
}
Run Code Online (Sandbox Code Playgroud)

这将使编写流输入/输出运算符更加容易(我知道您可以使用它friend来声明运算符)

c++ operator-overloading

8
推荐指数
1
解决办法
101
查看次数

标签 统计

c++ ×1

operator-overloading ×1