在C语言中,data和function是独立的entity,换句话说data和function是没有直接关联的。所以说使用C编程是面向过程编程,使用一个个的function来操作外在的data。而在C++中,通过Class将data和function集合在一起,形成一个Object。通过面向对象编程的三大特点:1)封装2)继承3)多态 来实现更好的灵活性。
1 |
|
c++ Object Memory Model
在上述代码中,Point类的大小跟C语言中的Point 结构体的大小是一致的,在64bit机器上使用sizeof运算符得到大小都是8 byte。考虑下面的一个例子:
1 | class A{ |
一个对象所占的内存空间是一块连续的地址,且后声明的数据成员具有更高的地址。
what is vtable
- static array usually the first slot in c++ object model
- class-specific
- vptr, vptr is assigned in constructor by compiler generated code, that is why member initialization list is different with initialization in constructor body.
- polymorphic can be implemented differently between compilers. But generally implement using vtable
- use sample code in linux, use gdb to show vtable: -exec info vtbl objectname
object memory model
- alignment
member function
- member function is just a tag in code segment, is not include in object memory layout
single inheritance and multi-inheritance
- pointer fix needed in multi-inheritance
notes
- pointers-to-members
- PPT material