当丢失非const虚拟方法时,为什么我不能实例化类的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()
?
是否允许使用
const_cast
?我可以在施放超级/子类的情况下遇到麻烦吗?
如果不是,那为什么必须定义它?
我认为有几种钝的方法可以放置:
-
,因为只有一个vtable。const和non-const对象共享。
-
,因为您可能会
const_cast
const const对象。 -
因为在构造对象时,它不是const。
-
因为constness不是对象本身的属性,所以它是附加到对象或引用的属性,它限制了对对象部分的访问。
其他问题是:
是否允许使用const_cast?
是。但是,您对const_casted参考的处理可能会迅速导致"未定义的行为"。这并不意味着"世界可能会结束",正如您相信的那样。它的含义是:" C 标准不能定义此行为 - 如果编译器,硬件或操作系统希望,这取决于它们"。即,您对该const_casted参考的方法不太可能是可移植的。
我可以在施放超级/子类时遇到麻烦吗?
演员是麻烦的开放邀请。
您无法实例化抽象类。故事的结尾。
相关文章:
- 从C++实例化QML
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 约束和显式模板实例化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 对象实例化调用构造函数的次数太多
- muQueue.front() 给出了 const 实例,即使我没有将其标记为 const
- 如何使用非默认构造函数实例化模板化类
- 静态数据成员模板专用化的实例化点在哪里
- 我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
- 无法用const char *数组实例化模板
- 如何处理 const T& 和 T&& 实例化到同一个签名?
- 当丢失非const虚拟方法时,为什么我不能实例化类的const实例
- 自定义类的const实例化是否也会使类中的*everything*变为常量
- 为什么无法使用复制构造函数实例化"non const"而可以在没有复制构造函数的情况下实例化配对?
- 通用成员函数定义,可从 'const' 和 'non-const' 对象实例化
- 优雅地重用将const添加到类实例化中的代码
- 使用constexpr数组的元素与const数组的元素实例化模板
- 如何让GCC用非平凡的const/dest实例化类实例