当我第一次使用 GCC 4.3 编译我的 C++ 代码时(在使用选项成功编译它并且在 4.1、4.0、3.4 上没有警告之后-Wall -Wextra
)我突然收到了一堆形式的错误warning: type qualifiers ignored on function return type
。
考虑temp.cpp
:
class Something
{
public:
const int getConstThing() const {
return _cMyInt;
}
const int getNonconstThing() const {
return _myInt;
}
const int& getConstReference() const {
return _myInt;
}
int& getNonconstReference() {
return _myInt;
}
void setInt(const int newValue) {
_myInt = newValue;
}
Something() : _cMyInt( 3 ) {
_myInt = 2;
}
private:
const int _cMyInt;
int _myInt;
};
运行g++ temp.cpp -Wextra -c -o blah.o
:
temp.cpp:4: warning: type qualifiers ignored on function return type
temp.cpp:7: warning: type qualifiers ignored on function return type
有人可以告诉我我做错了什么违反了 C++ 标准吗?我想当按值返回时,前导const
是多余的,但我无法理解为什么需要用它生成警告。还有其他地方我应该放弃 const 吗?