只是想知道为什么虚函数的语法在花括号之前使用const,如下所示:
virtual void print(int chw, int dus) const;
顺便说一句,代码似乎没有const,这很有趣..不知道为什么?
非常感谢!答案 0 :(得分:7)
函数签名中的const
表示 const成员函数 - Anthony Williams对其影响给出了great answer。
请注意,在这方面虚拟成员函数函数没有什么特别之处, constness 是一个适用于所有非静态成员函数的概念。
至于为什么它没有工作 - 你不能在const实例上调用非const成员。 E.g:
class C {
public:
void f1() {}
void f2() const {}
};
void test()
{
const C c;
c.f1(); // not allowed
c.f2(); // allowed
}