c++ - 返回 *this 会给出 Weffc++ 警告
问题描述
我在这里有一些代码,我正在使用 -Weffc++ -Wall -Wextra 进行编译。
基本上我有这个片段:
class base
{};
class test : public base
{
public:
base& operator=(int)
{
return *this;
}
};
我收到警告:warning: 'operator=' should return a reference to '*this' [-Weffc++]
。我真的不知道该怎么做这个警告。我已经读到这完全没问题(即返回一个尊重的 this)。
有没有办法让我的编译器满意?
解决方案
将您的代码更改为:
class test : public base
{
public:
test& operator=(int)
{
return *this;
}
};
每个人都会很高兴,不仅仅是你的编译器。
PS:如果您想了解更多 -Weffc++ 产生的警告是本书中建议的摘录:
有效的 C++:改进程序和设计的 55 种特定方法,Addison–Wesley,1992,(ISBN 0-321-33487-6)。
推荐阅读
- java - Guice - 有没有办法将依赖对象的新实例注入单例?
- java - 使物体更容易和更清洁
- node.js - throw new MongooseError('openUri()` 的 `uri` 参数必须是 ' +
- sql - 使用 LIKE 运算符时将数据类型 varchar 转换为 bigint 时出错
- c++ - 如何从函数修改对象?
- jquery - 使用 jQuery 在 nav-pills 上活动的类
- javascript - 侧边栏导航不适用于角度和物化 css
- android - 将移动应用程序中的离线数据同步到服务器时避免重复
- r - 带有 col_attributes 错误的 Simmer add_dataframe
- angular - Angular 无法取消订阅“.unsbscribe 不是函数”