对象的内部表示 [英] Internal representation of objects

查看:143
本文介绍了对象的内部表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有这一次,我认为当你做类似ObjectA.field1的时候,ObjectA就像栈上的任何值,你基本上访问它的字段。现在我正在通过一个关于OOP语言的类的笔记,意识到当你做ObjectA.field1实际发生是HEAP(AddressA的ObjectA)(field1),它返回你field1的值。这使我有点困惑。谁能告诉为什么有一个查找,虽然我们已经有对象的价值?希望我能够解释..

So all this time I thought that when you do something like ObjectA.field1, ObjectA is just like any value on the stack and you basically access its fields. Now I was going through the notes for a class about OOP languages and realized that when you do ObjectA.field1 what actually happens is HEAP(Address of ObjectA)(field1) which returns you the value of the field1. This makes me a bit confused. Can anyone tell why there is a look up going on although we already have the value of the object? Hope I was able to explain..

推荐答案

对象不是真的那么神奇。基本上,一个对象只是由所有成员的线性集合组成,在成员周围有未指定数量的填充。在布局方面,C ++类基本上就像一个C结构:

Objects aren't really that magical. Essentially, an object just consists of a linear collection of all its members, with unspecified amounts of padding surrounding the members. Layout-wise, a C++ class is essentially like a C struct:

struct Foo {
  int a;
  char b;
  std::string s;

  static long q;

  void bar() { print(s); log(a); }
  static void car() { }
}

忽略成员函数和现在,这可能是这样布局:

Ignoring member functions and statics for now, this might be laid out like this:

+= class Foo =+
+-------------+  ---\   <---   Foo * p
|  int        |     s
+-------------+     i
|  char       |     z
+-------------+     e
| <padding>   |     o
+-------------+     f
| std::string |    (F
+-------------+     o
| <padding>   |     o)
+-------------+  ---/

Foo 这样存储在内存中。我们需要的唯一额外数据是静态成员,成员函数和静态成员函数。

Every object of class Foo is stored like this in memory. The only extra data we need are the static members, member functions, and static member functions.

静态成员只是全局变量。因此,我们只有全局变量:

Static members are just global variables. So we have only one global variable:

+== static__Foo__q ==+
+--------------------+
|  long int          |
+--------------------+

接下来, static成员函数只是普通的自由函数:

Next up, static member functions are just ordinary, free functions:

void static__Foo__car() {  }

最后,成员函数只是普通函数,虽然有一个额外的参数,允许他们查找实例成员:

Finally, member functions: these are essentially also just ordinary functions, though with an extra parameter that allows them to find instance members:

void member__Foo__bar(Foo * p) { print(p->s); log(p->a); }

唯一重要的区别是,你不能获得一个普通的自由函数指针到成员函数,因为实际功能的实际名称没有公开。引用 Foo :: bar()的唯一方法是通过指向成员函数 void(Foo :: * ptfm) )=& Foo :: bar 。成员对象有点简单:你可以获得一个正常的指针,例如 Foo x; int * p =& xa; ,但你也可以形成一个指向成员的指针: int Foo :: * ptm =& Foo :: a; code>。

The only important difference is that you cannot obtain an ordinary free function pointer to member functions, since the actual name of the implementation function is not exposed. The only way to refer to Foo::bar() is via a pointer-to-member-function void (Foo::*ptfm)() = &Foo::bar. Member objects are a bit simpler: you can either obtain a normal pointer to them, like Foo x; int * p = &x.a;, but you can also form a pointer-to-member: int Foo::*ptm = &Foo::a;.

然后,如果我们有对象 Foo x,y,z; 使用实例指针 Foo * pi =& x; 和成员指针 int& Foo :: *的 ptm =& Foo :: a void(Foo :: * ptfm)()=& Foo :: bar 给定实例的成员:整数 pi-> * ptm ,函数调用(pi-> * ptfm) code>。 (是, - > * 是一个运算符。)

Then, if we have objects Foo x, y, z;, we can use the pairs of instance pointer Foo * pi = &x; and member pointers int &Foo::* ptm = &Foo::a or void (Foo::*ptfm)() = &Foo::bar to access the relevant member of the given instance: the integer pi->*ptm, and the function call (pi->*ptfm)(), respectively. (Yes, ->* is an operator.)

(函数指针的一个免费版本不能存在,因为与简单的固定函数指针相比​​,多态(虚拟)函数需要更复杂的调度机制。)

(A free version of the function pointer cannot exist, because polymorphic (virtual) functions require a more complicated dispatch mechanism than a simple, fixed function pointer.)

这篇关于对象的内部表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆