什么是“这个”的目的是什么,指针在C ++? [英] What is purpose of a "this" pointer in C++?
本文介绍了什么是“这个”的目的是什么,指针在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此
关键字的目的是什么?类中的方法不能访问同一类中的其他对等成员吗?需要调用 this
来调用类中的对等方法吗?
What is purpose of this
keyword. Doesn't the methods in a class have access to other peer members in the same class ? What is the need to call a this
to call peer methods inside a class?
推荐答案
两个主要用途:
-
要传递
* this
或作为其他非类方法的参数。
To pass
*this
orthis
as a parameter to other, non-class methods.
void do_something_to_a_foo(Foo *foo_instance);
void Foo::DoSomething()
{
do_something_to_a_foo(this);
}
MessageBox::MessageBox(const string& message)
{
this->message = message;
}
(虽然初始化列表通常优先于此特定示例中的分配。)
(Although an initialization list is usually preferable to assignment in this particular example.)
这篇关于什么是“这个”的目的是什么,指针在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文