类方法访问它的数据成员 [英] Class method access to it's data members
问题描述
我会用一个小代码来解释我的问题:
I would use a small code to explain my question:
class C {
int a;
public:
func() {
a = 4;
};
};
int main() {
C obj1;
obj1.func();
return 0;
}
这里 func()
方法尝试将 a
( obj1
的数据成员)设置为值4.如何 func()
访问 a
?是因为 func()
可以访问 this *
for obj1
?
如果是这样,如何访问
? func()
?是从 obj1.func()
时作为隐式参数传递给 func()
> main()
如果它作为参数传递, main()
$ c> this * 作为其局部变量存储?
如果否,如何和何时是 this *
和它在哪里存储?
Here func()
method tries to set a
(a data member of obj1
) to a value of 4. How does func()
get access to a
? Is it because func()
has access to this*
for obj1
?
If that's true, how does func()
get access to this*
? Is it passed to func()
as an implicit argument while calling obj1.func()
from main()
?
If it's passed as an argument, does main()
stack consist of a this*
stored as its local variable?
If no, how and when is the this*
pointer generated and where is it stored?
我试图根据我对主题的理解逐渐地提出问题。如果一切都是真的,我很困惑是否每个对象静态生成的堆栈,额外的这个*存储在堆栈。
I have tried to put across the question incrementally based on my understanding of the topic. If everything turns out to be true, I am confused whether for every object generated statically on the stack, an extra this* is stored on the stack.
请随意建议修改。
感谢。
推荐答案
这里的func()方法尝试将一个数据成员obj1设置为
func()如何访问'a'?是因为func()可以访问obj1的这个*?
Here func() method tries to set a(a data member of obj1) to a value of 4. How does func() get access to 'a'? Is it because func() has access to this* for obj1?
是的,你的假设是正确的。
Yes, your assumption is correct.
如果这是真的,func()如何访问这个*?它是作为一个隐式参数传递给
func()从main?调用obj1.func()?
If that's true, how does func() get access to this*? Is it passed to func() as an implicit argument while calling obj1.func() from main?
这将在本回复的稍后部分,但是是的。每个成员函数都传递一个self的参数。 C ++编译器为您执行此操作。请注意静态方法是一个例外。
I'll cover this more later in this reply, but yes. Every member function is passed an argument of self. The C++ compiler does this for you. Please note that static methods are an exception to this.
如果它作为参数传递,main()堆栈由一个这个*
存储为其局部变量?如果没有,如何和何时生成这个*
指针并且存储在哪里?
If it's passed as an argument, does main() stack consist of a this* stored as its local variable? If no, how and when is the this* pointer generated and where is it stored?
以下实施例。第一个在C中,第二个在CPP中。
Take a look at the following examples. The first is in C and the second is in CPP.
struct A{
int i;
}
increment(A* a){
a->i +=1;
}
int main(){
A anA;
anA.i = 0;
increment(&anA);
}
现在,查看cpp中的以下内容。
Now, look at the following in cpp.
struct A{
int i;
public:
void increment(){
i+=1;
}
}
int main(){
A anA;
anA.increment();
}
上面的例子在非常基本的性质上是一样的。他们执行相同的任务,并以相同的方式去做(排除诸如构造函数/析构函数)。主要的区别是,cpp编译器负责将指向你的模型(struct A)的指针传递给函数,在C中你需要自己传递它。
The examples above are, at a very basic nature, identical. They perform the same task and go about doing it in the same manner (excluding things like constructors/destructors). The main difference is that the cpp compiler takes care of passing a pointer to your model (struct A) to the function where in C you would need to pass it yourself.
这个*,
实际上只是一个指向你的struct的指针。所以在上面的例子中,它指向 anA
。
this*,
is really just a pointer to your struct. So in the example above its a pointer to anA
.
我建议在c ++中阅读思考。它是免费的,其中一本让我开始与CPP。如果你想要有一个很好的语言的价值的时间的理解。 此处。
I would suggest reading thinking in c++. It is free and one of the books that got me started with CPP. If you want to have a good understanding of the language its worth the time. here.
这篇关于类方法访问它的数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!