如何在C ++中为指针“ this”分配值 [英] How can you assign a value to the pointer 'this' in C++

查看:74
本文介绍了如何在C ++中为指针“ this”分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数中,如何为 this 分配新值?

In a function, how to you assign this a new value?

推荐答案

您不能。


1输入非静态(9.3)成员函数的主体,关键字 this prvalue 表达式,其值
是调用该函数的对象的地址。 [...](强调并链接我的)

1 In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. [...] (emphasis & link mine)

您可以修改对象 this 指向 * this 。例如:

You can modify the object this points to, which is *this. For example:

struct X
{
   int x;
   void foo()
   {
     this->x =3;
   }
};

该方法会修改对象本身,但是类似于 this = new X 是非法的。

The method modifies the object itself, but something like this = new X is illegal.

这篇关于如何在C ++中为指针“ this”分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆