-6
Q
类的数据结构
A
回答
3
编译器分配偏移的所有成员,包括这些上成员的所有加载/存储操作:
struct foo {
uint32_t bar;
uint32_t baz;
uint32_t get_baz() { return baz; }
};
uint32_t get_baz_from_foo(foo *f) { return f->baz; }
变为(用于简单ARM汇编代码):
foo__get_baz:
; calling convention: this pointer in r3
; load 32 bit value from r3 + 4 bytes into r0
ldr r0, [r3, #4];
; calling convention: return value in r0
; return from subroutine
b lr
get_baz_from_foo:
; calling convention: first parameter in r0
; load 32 bit value from r0 + 4 bytes into r0
ldr r0, [r0, #4]
; calling convention: return value in r0
; return from subroutine
b lr
作为struct
编译后各自的class
布局不变,这里4硬编码成指令流。
创建实例的工作方式分配内存,并从分配函数传递指针大家期待的指针结构:
new__foo:
; two 32 bit integers need 8 bytes
; calling convention: first parameter in r0
mov r0, #8
; call allocator, which will then return to the function invoking new
bra malloc
如果有一个构造
struct foo2 {
foo2() : bar(5), baz(7) { }
uint32_t bar;
uint32_t baz;
uint32_t get_baz() { return baz; }
};
我们结束用一种稍微更复杂的方式来创建对象(你应该能够弄清楚而没有评论):
new__foo2:
strdb lr, ![sp]
mov r0, #8
bl malloc
mov r1, #5
str r1, [r0]
mov r1, #7
str r1, [r0, #4]
ldaia lr, ![sp]
b lr
get_baz
的实现与foo
类相同。
现在,如果我构造这样一个对象,并获得巴兹值:
bl new__foo2
; remember: the this pointer goes to r3
mov r3, r0
bl foo2__get_baz
我结束了包含值7
r0
。
对于virtual
方法,创建隐藏的数据成员,这是一个指向的功能的表:
struct base {
virtual uint32_t get_baz() = 0;
};
struct derived : base {
derived() : baz(5) { }
virtual uint32_t get_baz();
uint32_t bar;
uint32_t baz;
};
变得
new__derived:
strdb lr, ![sp]
mov r0, #12
bl malloc
mov r1, #5
str r1, [r0, #8]
; get the address of the vtable
ldr r1, =vtable__derived
; vtable typically goes to the end of the class defining it
; as this is the base class, it goes before derived's data members
str r1, [r0]
ldria lr, ![sp]
b lr
vtable__derived:
; pointer to function
dw derived__get_baz
derived__get_baz:
ldr r0, [r3, #8]
b lr
调用此函数间接完成:
; construct normally
bl new__derived
; here, we forget that this is a "derived" object
; this pointer to r3
mov r3, r0
; get vtable ptr
ldr r0, [r3]
; get function ptr from vtable
ldr r0, [r0]
; call function
bl r0
这里,r0
现在是5
,因为那是构造函数存储在那里的。
相关问题
- 1. 数据结构的类型
- 2. 聚类结构3D数据
- 3. 类与数据结构
- 4. 数据结构来分类
- 5. Python分类 - 数据结构
- 6. Java多种类型的数据结构
- 7. 结构中的Dyanmic数据类型
- 8. 类似的数据结构栈
- 9. 的Java数据结构类型
- 10. 只有数据的类和结构?
- 11. Ada中的数据类型和结构
- 12. Mathematica中的结构数据类型?
- 13. 应用程序的类结构和数据库结构
- 14. Python类,数据结构和适当的体系结构
- 15. 为类似的数据结构选择MongoDB集合结构
- 16. 在Java中创建类似于数据结构的结构
- 17. 类与数据结构参数类C#团结
- 18. 数据结构
- 19. 数据结构
- 20. 数据结构
- 21. 数据结构++
- 22. 数据结构
- 23. 数据结构
- 24. 数据结构
- 25. 数字索引数据结构的最快数据结构?
- 26. 编译时数据数组结构以外的数据结构?
- 27. 递归数据结构走错了 - 从类型结构* L
- 28. 应该类结构镜像数据库结构?
- 29. python数据类型是类还是数据结构?
- 30. 分类和结构数据库
你到底在问什么? – IronMan84
'if'≠'jump'。至少,'if' ='compare' +'条件跳转'。一个类通常是一个数据结构,包含数据,指向该类的虚拟方法的指针以及指向该类祖先类(如果有的话)的指针。 –
C++类的数据结构是什么? – wizztjh