简体   繁体   English

在哪里放置这个指针,并在为它分配内存时?

[英]where is placed the this pointer and and when memory is allocated for it?

Why pointer to the virtual function table affects the size of the class , but this pointer does not affect the size of the class ? 为什么指向虚函数表的指针会影响类的大小,但是这个指针不会影响类的大小? where is placed the this pointer and when memory is allocated for it? 放置this指针的位置以及为其分配内存的位置?

It is passed as an implicit parameter and therefore it is not stored inside of the object. 它作为隐式参数传递,因此不存储在对象内部。 When you write: 当你写:

struct X
{
    void f( int i );
};

X x;
x.f( 42 );

You can think of it like this: 你可以这样想:

void f( X* const this, int i ); // of course this would be illegal as "this" is a keyword

f( &x, 42 );

So the this -pointer is coming from where the method is called. 所以this -pointer来自调用方法的地方。

The pointer to the virtual function table is stored in the object, but this is actually a hidden parameter of member functions. 的指针虚拟函数表存储在该对象,但是this实际上是成员函数隐藏参数 In effect, when you have this: 实际上,当你有这个:

class X
{
  void foo(int bar) const;
};

int main()
{
  X x;
  x.foo(7);
}

Then the compiler will implicitly transform it to something like this (pseudo-code): 然后编译器会隐式地将它转换为类似的东西(伪代码):

class X
{
  static void foo(const X * const this, int bar);
};

int main()
{
  X x;
  X::foo(&x, 7);
}

Note that I am only showing the pseudo-code for illustration purposes. 请注意,我只是为了说明目的而显示伪代码。 You can conceptually imagine it like that, but it's not what is actually happening. 你可以在概念上想象它,但它并不是实际发生的事情。

this pointer is implicitly placed on the stack (it's like an argument to the method). this指针隐式放在堆栈上(它就像是方法的参数)。 So when you call obj->foo() you're implicitly passing obj into method foo. 所以当你调用obj->foo()你会隐式地将obj传递给方法foo。 The compiler gives you access to that argument through this . 编译器允许您通过this访问该参数。

In general there can't be a need to store this pointer inside the object anyway. 通常,无论如何都不需要将该指针存储在对象内。 It would be redundant (ie if you could get hold of the object its address is already available to you). 这将是多余的(即,如果您可以抓住该对象,则其地址已经可供您使用)。

vTable however is a different story. 然而,vTable是一个不同的故事。 Which vtable an object points to can't be known at compile time and there's no separate implicit argument either. 在编译时无法知道对象指向哪个vtable,也没有单独的隐式参数。 This requires dedicated space in the object itself. 这需要对象本身的专用空间。

就像'this'指针一样,vptr依赖于一个实例而不是类。

Nowhere. 无处。 The this pointer isn't an lvalue, and doesn't occupy memory as such. this指针不是左值,并且不会占用内存。 It's sort of like asking where the results of x + y reside (where x and y are int ). 这有点像询问x + y的结果所在的位置(其中xyint )。 Depending on the implementation, where it will really reside will vary: VC++, for example, tends to keep it in the register ECX (although this may vary when optimization is used); 根据实现情况,它实际存在的位置会有所不同:例如,VC ++倾向于将其保留在寄存器ECX中(尽管使用优化时可能会有所不同); Sun CC for Sparc puts it in the register i0 , etc. It's not a member, even implicit, of the class. Sun CC for Sparc将它放在寄存器i0等中。它不是该类的成员,甚至是隐含的。 It's probably best thought of as an argument to the function, although many compilers will treat it special, and unlike normal arguments, it's not an lvalue. 它可能最好被认为是函数的一个参数,尽管许多编译器会将它视为特殊的,并且与普通参数不同,它不是左值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM