关于重载运算符

灌水区

@[xie_T34](/user/846661) 应该是一样的,就是方法不同
by zxjn @ 2024-03-24 15:32:47


@[xie_T34](/user/846661) 相当于给 `this` 加了个 `const`
by XuYueming @ 2024-03-24 15:33:08


thx
by A_R_O_N_A @ 2024-03-24 15:52:33


@[XuYueming](/user/728079) 应该是相当于给 `x` 加了个 `const`…… `this` 参数是隐式传递的。若想要使 `this` 不可变(即方法不改变对象的任何内容),有以下两种方法可行: ### `const` 方法(任意 C++ 版本) 如代码所示: ```cpp class Edge { public: int u; int v; int w; inline bool operator<(const Edge& that) const { return w < x.w; } }; ``` ### Decuding this 方法(针对 C++23) 如代码所示: ```cpp class Edge { public: int u; int v; int w; inline operator<(this const Edge& self, const Edge& that) { return self.w < x.w; } }; ```
by CleanIce @ 2024-03-24 17:50:56


@[CleanIce](/user/821660) 我指的是 > ``` > bool operator <(const edge x)const{ > ^^^^^ > ```
by XuYueming @ 2024-03-24 18:13:10


|