何时是“这个”指针在C ++中初始化? [英] When is "this" pointer initialized in C++?

查看:135
本文介绍了何时是“这个”指针在C ++中初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一个关于这个指针的问题,当一个对象被构造,当它被初始化?这意味着,我什么时候可以使用它?虚拟表是在构造函数中构造的,与 this 指针相同?



代码这样。输出是8.是否意味着在输入构造函数之前,指针已初始化?

  class A {
public:
A(){cout< int i;
int * p;
};

int main(){
A a;
}

如果为真,在构造函数输入之前还会发生什么? / p>

如果不是真的,这个指针是否初始化?

c> 你打电话。



在上面的例子中,构造函数是一个特殊的方法,它又是一种特殊的功能。当你构造对象时,编译器为它分配内存(在这种情况下在栈上, a main 函数,然后它自动调用构造函数来初始化对象。



作为调用构造函数的一部分,隐式参数 this



在具有以下签名的方法中...

  void MyMethod(const int * p)const; 

实际上有两个参数,两个指针。显式参数 p 和隐式参数 this const 在结束处指定这个是一个常量指针,前面的一个指定 p 是一个常量指针,只需要隐式传递 this 所以你不能像使用其他参数一样以正常的方式指定常量。



静态方法没有隐式的this参数,不能直接访问对象成员 - 可能没有与调用关联的特定对象。它基本上是一个标准函数,而不是一个方法,除了访问私有成员(只要它可以找到一个对象来访问)。



正如Steve Fallows指出的, code> sizeof(this)在编译时是已知的,因为它是一个指针类型,所有指针(* 1)都有相同的 sizeof value。你看到的8意味着你正在编译一个64位平台。 这个在这一点上是可用的 - 它指向有效的内存,并且所有成员已经完成了它们的构造函数调用。



EDIT



* 1 - 严格来说,这可能不是真的 - 但是编译器知道这里处理的是什么类型的指针,即使该值在运行时还未知。


Hi I have a question about this pointer, when an object is constructed, when it is initialized? Which means, when can I use it? The virtual table is constructed in the constructor, is the same with this pointer?

For example, I have a code like this. The output is 8. Does it mean that before the constructor is entered, this pointer is already initialized?

class A{
   public:
     A() { cout<<sizeof(*this);}
     int i;
     int *p; 
};

int main() {      
    A a; 
}

If it is true, what else would happen before the constructor is entered ?

If it is not true, when is the this pointer initialized ?

解决方案

The this pointer isn't a member of the object or class - it's an implicit parameter to the method that you call. As such it's passed in much like any other parameter - except that you don't directly ask for it.

In your example above, the constructor is a special method, which is in turn a special kind of function. When you construct the object, the compiler allocates memory for it (in this case on the stack, as a is a local variable in the main function. Then it automatically calls the constructor to initialise the object.

As part of calling the constructor, the implicit parameter this - a pointer to your object - is passed in as a parameter.

In a method with the following signature...

void MyMethod (const int* p) const;

there are actually two parameters, both pointers. There's the explicit parameter p and the implicit parameter this. The const at the end of the line specifies that this is a const pointer, much as the earlier one specifies that p is a const pointer. The need for that special syntax only exists because this is passed implicitly, so you can't specify const-ness in the normal way as with other parameters.

A "static" method doesn't have the implicit "this" parameter, and cannot directly access the object members either - there may not be a particular object associated with the call. It is basically a standard function rather than a method, except with access to private members (providing it can find an object to access).

As Steve Fallows points out, sizeof (this) is known at compile-time, because it is a pointer type and all pointers (*1) have the same sizeof value. The "8" you see implies you are compiling for a 64-bit platform. this is usable at this point - it points to valid memory, and all the members have completed their constructor calls. However, it isn't necessarily fully initialised - you are still in the constructor call after all.

EDIT

*1 - strictly, that may not be true - but the compiler knows what type of pointer it's dealing with here even though the value isn't known until runtime.

这篇关于何时是“这个”指针在C ++中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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