C++ g++:const 丢弃限定符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2412608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++: const discards qualifiers
提问by Josh Kelley
why do I get a discard qualifierserror:
为什么会出现discard qualifiers错误:
customExc.cpp: In member function ‘virtual const char* CustomException::what() const':
customExc.cpp: error: passing ‘const CustomException' as ‘this' argument of ‘char customException::code()' discards qualifiers
on the following code example
在以下代码示例中
#include <iostream>
class CustomException: public std::exception {
public:
virtual const char* what() const throw() {
static std::string msg;
msg = "Error: ";
msg += code(); // <---------- this is the line with the compile error
return msg.c_str();
}
char code() { return 'F'; }
};
I have searched around on SOF before regarding simular issues.
我之前在 SOF 上搜索过类似的问题。
I have already added a conston every possible place.
我已经const在每个可能的地方添加了一个。
Please enlighten me - I don't get the point...
请赐教 - 我不明白这一点......
EDIT: here are the steps to reproduce on Ubuntu-Carmic-32bit (g++ v4.4.1)
编辑:这里是在 Ubuntu-Carmic-32bit (g++ v4.4.1) 上重现的步骤
- save example as
customExc.cpp - type
make customExc.o
- 将示例另存为
customExc.cpp - 类型
make customExc.o
EDIT: The error is related to CustomException. The class Foohas nothing to do with it. So I have deleted it.
编辑:该错误与CustomException. 类Foo与它无关。所以我已经删除了。
回答by Josh Kelley
CustomException::whatcalls CustomException::code. CustomException::whatis a const method, as signified by the const afterwhat(). Since it is a const method, it cannot do anything that may modify itself. CustomException::codeis not a const method, which means that it does notpromise to not modify itself. So CustomException::whatcan't call CustomException::code.
CustomException::what调用CustomException::code。 CustomException::what是一个 const 方法,由后面的 const 表示what()。因为它是一个 const 方法,所以它不能做任何可能修改自己的事情。 CustomException::code不是一个常量方法,该方法的装置,它并不能保证不修改本身。所以CustomException::what不能打电话CustomException::code。
Note that const methods are not necessarily related to const instances. Foo::barcan declare its excvariable as non-const and call const methods like CustomException::what; this simply means that CustomException::whatpromises not to modify exc, but other code might.
请注意,const 方法不一定与 const 实例相关。 Foo::bar可以将其exc变量声明为非常量并调用 const 方法,例如CustomException::what;这仅仅意味着CustomException::what承诺不会修改exc,但其他代码可能会。
The C++ FAQ has a bit more information on const methods.
C++ FAQ 有更多关于const 方法的信息。
回答by amit
int code() const { return 42; }
回答by AnT
Your code()member function is not declared const. Calling non-const member functions from const member functions (what()in this case) is illegal.
您的code()成员函数未声明const。从 const 成员函数(what()在本例中)调用非常量成员函数是非法的。
Make your code()member const.
使您的code()成员成为常量。
回答by leiz
Your what()is a const member function, but code()is not.
你what()是一个 const 成员函数,但code()不是。
Just change code()to code() const.
只需更改code()为code() const.

