【问题标题】:returning *this gives Weffc++ warning返回 *this 会给出 Weffc++ 警告
【发布时间】:2018-09-04 18:17:07
【问题描述】:

我有一些代码 here 正在使用 -Weffc++ -Wall -Wextra 进行编译。

基本上我有这个sn-p:

class base
{};

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

我收到警告:warning: 'operator=' should return a reference to '*this' [-Weffc++]。我真的不知道该怎么做这个警告。我读过这完全没问题(即返回一个尊重的 this)。

有什么方法可以让我的编译器满意吗?

【问题讨论】:

标签: c++ c++11 g++ weffc++


【解决方案1】:

将您的代码更改为:

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

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

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

Effective C++:改进程序和设计的 55 种特定方法, Addison-Wesley,1992,(ISBN 0-321-33487-6)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多