简体   繁体   English

C++ 堆栈帧/激活记录和“this”指针

[英]C++ Stack Frame/Activation Record and 'this' pointer

This is my first post here, if I am messing something up I would appreciate any advice.这是我在这里的第一篇文章,如果我搞砸了,我会很感激任何建议。 Does C++ implement an activation record? C++ 是否实现了激活记录? My experience with this has been largely with Java so I am unsure if it is the same for other languages.我在这方面的经验主要是 Java,所以我不确定其他语言是否也一样。 If so, in C++ is it correct to say that 'this' is a pointer to the previous record on the activation stack?如果是这样,在 C++ 中说“this”是指向激活堆栈上前一条记录的指针是否正确? For example:例如:

...
class Example {
private:
    int num;
public:
    void setNum(int num) {
        this->num = num;
    }
...

So the activation stack would have an 'Example' object on the stack and then when the function 'setNum(...)' is called it would put that call on the activation stack.因此,激活堆栈将在堆栈上有一个“示例”object,然后当调用 function 'setNum(...)' 时,它将将该调用放在激活堆栈上。 So 'this' would be of type Example* and would point to the 'Example' object that is on the stack before the function call.因此,“this”的类型为 Example*,并指向 function 调用之前堆栈中的“Example” object。 Is that correct?那是对的吗? Thanks all!谢谢大家!

this only exists within a class or struct . this仅存在于classstruct中。 It does not exists within a free function.它不存在于免费的 function 中。

this points to the object whose member function is called. this指向 object,其成员 function 被调用。

In your case, this points to an instance of Example and thus the type is Example*在您的情况下, this指向 Example 的Example ,因此类型为Example*

I don't know the term ActivationRecord .我不知道ActivationRecord这个词。 C++ doesn't know the concept of a function stack, that's just an implementation detail. C++ 不知道 function 堆栈的概念,这只是一个实现细节。

Everything is very stright in с++. с++ 中的一切都非常简单。 this will be pointer to current object(instance of class Example ). this将是指向当前对象的指针( class Example的实例)。 And if setNum can be called then you already have this object.如果可以调用setNum ,那么您已经有了这个 object。 You do not need this for setting value to any memeber.您不需要为任何成员设置值。

class Example {
private:
    int num;
public:
    void setNum(int _num) {
        num = _num;
    }
};

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

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