C++コードをGCC4.3で初めてコンパイルしたとき(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をやめるべき他の場所はありますか?