- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
最近,我一直在阅读“inside the c++ object model”。它说 vptr 在调用基类的构造函数后初始化。所以我进行了测试:
class A {
public:
A(int i) {
std::cout << i << std::endl;
}
virtual int vfunc() {
return 1;
}
};
class B : public A {
public:
B() : A(vfunc()) {
}
virtual int vfunc() {
return 2;
}
};
int main() {
B b;
}
结果如下:
2
我的问题是,类 B 是否在调用基类 A 的构造函数之前先设置其 vptr?
最佳答案
您的问题的答案是否。如果在父构造函数执行之前设置 vptr
,则意味着该构造函数将覆盖它。
至于您在代码中看到的行为:在其构造函数内对正在构造的对象的虚函数的任何调用都是在没有 vptr
的情况下解析的。所以你的代码实际上等同于:
B() : A(B::vfunc()) {
}
没有虚拟通话。相关标准写法([class.cdtor]p3):
Member functions, including virtual functions (13.3), can be called during construction or destruction (15.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class.
关于c++ - 什么时候为派生类初始化 vptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52831064/
我正在阅读 Inside C++ Object Model 一书中与对象破坏相关的主题并遇到了这个问题。 它表示在执行用户自定义析构函数之前,析构函数将被扩充。扩充的第一步是将 vptr 指针重置为该
在许多资料中,书籍等都写着“不要在子类构造函数中调用 this->virtualFunction”,并且在一些资料中解释了为什么你不能这样做。因为目前根本没有创建构造类。因此,将从子构造函数调用的虚函
我有一个带有虚拟析构函数的类“base”,因此其中有一个 VTable 和相应的 VTPR,以及一个从它派生的类: class base { public: virtual ~base() {
为什么我们不对非虚函数使用相同的方法? 我的意思是,为什么我们要那样使用虚函数?我们不能将它们用作非虚拟的并覆盖它们吗? 如果这种方法可以节省我们的时间/空间或其他任何东西,为什么我们不对非虚函数使用
vptr 索引应该显示所有虚函数,但在我的例子中,3 个虚函数中只显示了 2 个。 我在下面提供完整的代码和屏幕截图:- 类头.h #include using namespace std; //
class base { public: void virtual fn(int i) { cout fn(i); cout fn(i) 调用;没事。 我的观点是,如果
我在看 this article ,它说“进入基类析构函数后,该对象成为基类对象,并且 C++ 的所有部分——虚函数、dynamic_casts 等——都以这种方式对待它。”这是否意味着 vptr 在
这与之前的许多问题类似,但它提出了一些我无法找到答案的问题。 #include using namespace std; class Base1 { public: int
最近,我一直在阅读“inside the c++ object model”。它说 vptr 在调用基类的构造函数后初始化。所以我进行了测试: class A { public: A(int
以上是一道面试题。 据我了解,作为虚拟分派(dispatch)机制的一部分,编译器会为每个类创建一个vTable,并在编译期间插入一个额外的指针(vptr)。但是它究竟是什么时候将类的虚拟表分配给这个
在c++中,我们在每个对象中都有VPTR,但每个类只有一个VTABLE。为什么 VPTR 在每个对象中?是不是重复了? 最佳答案 如果没有运行时类型信息,您将不知道对象属于哪个类。因此,为了调用方法,
有些东西我还是不明白。 对于我声明的每个类,都有一个指向类虚拟表的隐藏 vptr 成员。 假设我有这个声明: class BASE { virtual_table* vptr; //that'
根据 MSDN,__RTDynamicCast() function用于在 Visual C++ 中实现 dynamic_cast。它的参数之一是 LONG VfDelta,它被描述为“对象中虚函数指
任何具有虚函数的类都会获得一个额外的隐藏指针,该指针将指向最派生的类。 这个vptr的类型是什么? 最佳答案 它没有类型。这是标准未指定的实现细节; 它不是语言的一部分。 请注意,C++ 根本没有说必
我想知道是否存在编译器不需要将 vptr 分配给实例化对象的可能优化,即使该对象的类型是具有虚方法的类。 例如考虑: #include struct FooBase { virtual void
我正在上计算机安全课并且正在阅读 http://phrack.org/issues/56/8.html .在 bo3.cpp 中,作者创建了他自己的 VTABLE,并覆盖 VPTR 以指向他的 VTA
我有几个类(class),我试图了解 vptr 和 vtable 在这种情况下是如何工作的。 class RGB { short value[3]; }; class AbstractImage{
我想知道为什么在此处的一种情况下我在 delete 部分得到异常,而在另一种情况下却没有。 没有异常(exception)情况 #include using namespace std; class
对于一些编译器,如果一个类有虚函数,那么它的 vptr 可以用它的对象的第一个字节的地址来访问。例如, class Base{ public: virtual void f(){cout<<"
这个问题不是关于 C++ 语言本身(即不是关于标准),而是关于如何调用编译器来实现虚函数的替代方案。 实现虚函数的一般方案是使用指向指针表的指针。 class Base { private:
我是一名优秀的程序员,十分优秀!