什么是'this'指针?
What is the 'this' pointer?
我对c++相当陌生,我不明白this
指针在以下场景中的作用:
void do_something_to_a_foo(Foo *foo_instance);
void Foo::DoSomething()
{
do_something_to_a_foo(this);
}
我从别人的帖子里抄的。
this
指向什么?我困惑。函数没有输入,那么this
在做什么?
this
为当前对象。
关键字this
标识了一种特殊类型的指针。假设您创建了class A
的一个名为x
的对象,并且class A
有一个非静态成员函数f()
。如果调用函数x.f()
,则f()
体中的关键字this
存储了x
的地址。
简短的回答是,this
是一个特殊的关键字,用于标识"此"对象-您当前正在操作的对象。稍微长一点、更复杂一点的答案是:
当你有class
时,它可以有两种类型的成员函数:static
和非static
。非static
成员函数必须对类的特定实例进行操作,并且它们需要知道该实例在哪里。为了帮助他们,语言定义了一个隐式变量(即一个在需要时自动为你声明的变量,而不需要你做任何事情),它被称为this
,它将自动指向成员函数所操作的类的特定实例。
考虑这个简单的例子:
#include <iostream>
class A
{
public:
A()
{
std::cout << "A::A: constructed at " << this << std::endl;
}
void SayHello()
{
std::cout << "Hi! I am the instance of A at " << this << std::endl;
}
};
int main(int, char **)
{
A a1;
A a2;
a1.SayHello();
a2.SayHello();
return 0;
}
当您编译并运行它时,可以观察到this
的值在a1
和a2
之间是不同的。
只是一些关于this
的随机事实,以补充其他答案:
class Foo {
public:
Foo * foo () { return this; }
const Foo * cfoo () const { return this; /* return foo(); is an error */ }
};
Foo x; // can call either x.foo() or x.cfoo()
const Foo y; // can only call x.cfoo()
当对象为const
时,this
的类型变为指向const
的指针。
class Bar {
int x;
int y;
public:
Bar () : x(1), y(2) {}
void bar (int x = 3) {
int y = 4;
std::cout << "x: " << x << std::endl;
std::cout << "this->x: " << this->x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "this->y: " << this->y << std::endl;
}
};
this
指针可用于访问被函数形参或局部变量遮蔽的成员。
template <unsigned V>
class Foo {
unsigned v;
public:
Foo () : v(V) { std::cout << "<" << v << ">" << " this: " << this << std::endl; }
};
class Bar : public Foo<1>, public Foo<2>, public Foo<3> {
public:
Bar () { std::cout << "Bar this: " << this << std::endl; }
};
多重继承将导致不同的父节点具有不同的this
值。只有第一个继承的父对象具有与派生对象相同的this
值
this是指向self(调用this的对象)的指针。
假设你有一个名为Car的类对象,它有一个非静态方法getColor(),在getColor()中调用这个方法会返回Car的地址(类的实例)。
静态成员函数没有this指针(因为它们与实例无关)。
表示调用DoSomething()的Foo对象。我用例子来解释
void do_something_to_a_foo(Foo *foo_instance){
foo_instance->printFoo();
}
和我们的类
class Foo{
string fooName;
public:
Foo(string fName);
void printFoo();
void DoSomething();
};
Foo::Foo(string fName){
fooName = fName;
}
void Foo::printFoo(){
cout<<"the fooName is: "<<fooName<<endl;
}
void Foo::DoSomething(){
do_something_to_a_foo(this);
}
现在我们实例化像
这样的对象Foo fooObject("first);
f.DoSomething();//it will prints out first
同样,无论传递给Foo构造函数的字符串是什么,都将在调用DoSomething()时打印出来。
因为例如在上面的DoSomething()中,"this"表示fooObject,而在do_something_to_a_foo()中,fooObject是通过引用传递的
Acc。用c++实现面向对象编程
this
是指向调用this
函数的对象的指针。例如,函数调用A.max()
将把指针this
设置为对象的地址。指针this
is作为所有成员函数的隐式实参。
你会在这里找到一个很好的this
指针的例子。这也帮助我理解了这个概念。http://www.learncpp.com/cpp-tutorial/8-8-the-hidden-this-pointer/
非静态成员函数(如Foo::DoSomething
)有一个隐式形参,其值用于this
。标准在c++ 11§5.2.2/4中对此进行了规定:
因此,需要一个调用函数时,每个形参(8.3.5)都要用对应的实参初始化(8.5,12.8,12.1)。如果函数是一个非静态成员函数,则该函数(9.3.2)的
this
形参应初始化为指向调用对象的指针,就像通过显式类型转换(5.4)一样进行转换。
Foo
对象来调用DoSomething
。该对象直接变为this
。
this
关键字与普通的显式声明的const
指针参数之间的唯一区别(而且是微不足道的)是,您不能使用this
的地址。
是一个局部指针。它将当前对象引用为本地对象
- 为什么使用 "this" 指针调用派生成员函数?
- 关于C++中具有多重继承"this"指针的说明
- Doees the 'this' 指针参与虚函数的多态行为
- 指针'this+1' C++中指的是什么?
- 为什么使用指向函数的指针调用虚函数时不需要指针"this"?
- Lambda 函数捕获错误的指针"this"
- 调用类的方法时,类的指针"this"变为 null C++
- 指针'this'可以是共享指针吗?
- 如何在C++中为指针'this'赋值
- C++:指针"this"没用吗?
- 指针"this"有什么问题?
- 将指针'this'传递为 LPARAM
- 错误 C2663:指针'this'重载没有合法转换
- C++ : 了解指针"this"
- 更改对象的指针'this'以指向不同的对象
- C++编译器是为所有成员方法生成指针"this"还是仅为引用成员的方法生成指针?
- 使用指针和不使用指针"this"有区别吗?
- 将enable_shared_from_这个基于类的指针(this)存储在itelf中
- 创建指向对象的指针"this"
- 如何在类中取消初始化指针"this"?