为什么在缺少非 const 虚拟方法时不能实例化 class 的 const 实例?

Why can I not instantiate a const instance of a class when non-const virtual methods are missing?

为什么这不合法?

class Base {
public:
    virtual void bar() = 0;
    virtual void barc() const = 0;
};

class Derived: public Base {
public:
    // deliberately omitted
    // virtual void bar()
    virtual void barc() const override { };
};

int main() {
    const Derived b;
    b.barc();
    // b.bar();
    // fails as expected with "passing 'const Bar' as 'this' argument discards qualifiers"
}

失败并出现以下错误,因为我没有定义 virtual void bar():

prog.cpp: In function 'int main()':
prog.cpp:16:12: error: cannot declare variable 'd' to be of abstract type 'Derived'
  const Derived d;
            ^
prog.cpp:10:7: note:   because the following virtual functions are pure within 'Derived':
 class Derived : public Base{
       ^
prog.cpp:6:15: note:    virtual void Base::bar()
  virtual void bar() = 0;

但无论如何我都不能在这里调用 bar,因为那个方法没有被标记为 const。如果定义了 bar,是否有任何合法的方式来间接调用 d.bar()

如果不是,那我为什么要定义它?

我认为有一些直接的表达方式:

  1. 因为只有一个vtable。 const 和非常量对象共享它。

  2. 因为您可能 const_cast 将 const 对象转换为非常量对象。

  3. 因为在构造对象的时候,它不是const.

  4. 因为 constness 不是对象本身的 属性,它是附加到对象或限制对对象部分的访问的引用的 属性。

进一步的问题是:

Is using a const_cast allowed?

是的。但是您对 const_casted 引用所做的操作会很快导致 "undefined behaviour"。这并不意味着 "the world may end",有些人会让您相信。它的意思是,"the c++ standard does not define this behaviour - if the compiler, the hardware or the OS wish to, that's up to them"。也就是说,无论你用那个 const_casted 参考做什么,都不太可能是可移植的。

Can I get in trouble with casting a super/subclass?

强制转换是自找麻烦。

您不能实例化抽象 class。故事结束。