問題描述
我對 C++ 相當陌生,我不明白 this
指針在以下場景中的作用:
I'm fairly new to C++, and I don't understand what the this
pointer does in the following scenario:
void do_something_to_a_foo(Foo *foo_instance);
void Foo::DoSomething()
{
do_something_to_a_foo(this);
}
我從這里其他人的帖子中獲取的.
I grabbed that from someone else's post on here.
this
指向什么?我糊涂了.函數(shù)沒有輸入,那么this
在做什么?
What does this
point to? I'm confused. The function has no input, so what is this
doing?
推薦答案
this
引用當前對象.
關鍵字this
標識了一種特殊類型的指針.假設你創(chuàng)建了一個class A
的名為x
的對象,并且class A
有一個非靜態(tài)成員函數(shù)f()代碼>.如果調用函數(shù)
xf()
,f()
主體中的關鍵字this
存儲的是x
的地址代碼>.
The keyword this
identifies a special type of pointer. Suppose that you create an object named x
of class A
, and class A
has a non-static member function f()
. If you call the function x.f()
, the keyword this
in the body of f()
stores the address of x
.
這篇關于“this"指針是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!