[转] When exactly does the virtual table pointer (in C++) gets set for an object?
PS: http://stackoverflow.com/questions/7934540/when-exactly-does-the-virtual-table-pointer-in-c-gets-set-for-an-object
This is strictly Implementation dependent.
For Most compilers,
The compiler initializes this->__vptr within each constructor's Member Initializer list.
The idea is to cause each object's v-pointer to point at its class's v-table, and the compiler generates the hidden code for this and adds it to the constructor code. Something like:
Base::Base(...arbitrary params...)
: __vptr(&Base::__vtable[0]) ← supplied by the compiler, hidden from the programmer
{
}
This C++ FAQ explains a gist of what exactly happens.
This msdn article explains it in great detali
There it says :
"And the final answer is... as you'd expect. It happens in the constructor."
If I might add, right at the beginning of the constructor, before any other code you might have in your constructor gets executed.
But be careful, let's say you have the class A, and a class A1 derived from A.
- If you create a new A object, the vptr will be set right at the beginning of the constructor of the A class
- But if you create a new object A1:
"Here's the entire sequence of events when you construct an instance of class A1:
- A1::A1 calls A::A
- A::A sets vtable to A's vtable
- A::A executes and returns
- A1::A1 sets vtable to A1's vtable
- A1::A1 executes and returns "
[转] When exactly does the virtual table pointer (in C++) gets set for an object?的更多相关文章
- C++: Virtual Table and Shared Memory
See at: 补充栏3: C++对象和共享内存 (叙述内容和Link1的内容基本一致) <C++网络编程 卷1:运用ACE和模式消除复杂性> <C++ Network Progra ...
- virtual table(有180个评论)
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...
- virtual table for class
虚函数表 说起虚函数,相信你我都可以自然而然的想到“多态”,因为多态的实现就依赖于虚函数的继承和重写(覆盖).那么,class又或者是object是如何来管理虚函数的呢?你我又会想到虚函数表. 虚函数 ...
- (C/C++学习)4.C++类中的虚函数表Virtual Table
说明:C++的多态是通过一张虚函数表(Virtual Table)来实现的,简称为V-Table.在这个表中,主要为一个类的虚函数的地址表,这张表解决了继承.覆写的问题,保证其真实反应实际的虚函数调用 ...
- Virtual Table
C++对象模型——吴泰 C/C++杂记 C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现 C++ 多继承和虚继承的内存布局 [已翻译100%] (虚继承参考,推荐) 图说C++对象模型:对 ...
- C++对象的virtual table在内存中的布局
(1)单一继承 (2)多重继承 (3)虚拟继承 参考:<深度探索C++对象模型>
- A Base Class pointer can point to a derived class object. Why is the vice-versa not true?
问题转载自:https://stackoverflow.com/questions/4937180/a-base-class-pointer-can-point-to-a-derived-class- ...
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
遇到这个一场折腾了1个小时, 这是系统在解析XML的时候出错, 最后费了好大的劲才发现 XML文件中,<View> 写成小写的 <view> 了. 崩溃啊.......... ...
- [Effective C++系列]-为多态基类声明Virtual析构函数
Declare destructors virtual in polymorphic base classes. [原理] C++指出,当derived class对象经由一个由base clas ...
随机推荐
- 创建cocos项目
创建cocos项目 进入终端输入命令: cocos new HelloCpp -p com.cocos.org -l cpp -d /Users/emerys/documents/cocos 参数说明 ...
- 『重构--改善既有代码的设计』读书笔记----Introduce Explaning Variable
有时候你会遇到一系列复杂的表达式连续运算的时候,这个时候你可能根本招架不住如此长或者是如此复杂的长函数.这个时候你可以通过引用临时变量来储存他们的结果,将这些长函数的结果分成一个个临时变量来让函数清晰 ...
- Linux 0.11下信号量的实现和应用
Linux 011下信号量的实现和应用 生产者-消费者问题 实现信号量 信号量的代码实现 关于sem_wait和sem_post sem_wait和sem_post函数的代码实现 信号量的完整代码 实 ...
- javascript 16/1/14随记
1.想在一个事件或者函数之后,触发某个事件. var flag=false //定义一个全局变量 function aku(){ if(flag){ } } sizemousedown=functio ...
- PHP错误报告级别及调整方法
运行PHP脚本时,PHP解析器会尽其所能能报告它遇到的问题.在PHP中错误报告的处理行为,都是通过PHP的配置文件php.ini中有关的配置指令确定的.另外PHP的错误报告有很多种级别,可以根据不同的 ...
- Python学习笔记:03语法
Python 语法 Python语法包括: 模块函数导入 赋值 判断循环语句 模块导入 import somemodule somemodule.somefunc from somemodule im ...
- link与@import
导入外部样式的两种写法 <link rel="stylesheet" href="xxxx.css"> <style> @import ...
- python对比两个文件问题
写一个比较两个文本文件的程序. 如果不同, 给出第一个不同处的行号和 列号. 比较的时候可以使用zip()函数 a=open('test.txt','r') b=open('test2.txt','r ...
- Scut:通用配置管理器
1. 配置节 ConfigSection private List<ConfigNode> _configNodes; public class ConfigNode { public C ...
- ios学习-delegate、传值、跳转页面
ios学习-delegate.传值.跳转页面 1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...