返回 *这会给出 Weffc++ 警告

returning *this gives Weffc++ warning

本文关键字:Weffc++ 警告 返回      更新时间:2023-10-16

我这里有一些代码,我正在使用-Weffc++ -Wall -Wextra编译。

基本上我有这个片段:

class base
{};
class test : public base
{
public:
base& operator=(int)
{
return *this;
}
};

我得到警告:warning: 'operator=' should return a reference to '*this' [-Weffc++].我真的不确定该如何看待这个警告。我已经读到这是完全可以的(即返回一个尊重这个(。

有没有办法让我的顺从者满意?

将代码更改为:

class test : public base
{
public:
test& operator=(int)
{
return *this;
}
};

每个人都会很高兴,而不仅仅是你的编译器。

PS:如果你想了解更多,-Weffc++产生的警告是本书中建议的摘录:

有效C++:改进程序和设计的55种具体方法, 艾迪生-韦斯利,1992年,(国际标准书号0-321-33487-6(。