Luc*_*ore 30
你不能.
1在非静态(9.3)成员函数的主体中,关键字
this是一个prvalue表达式,其值是调用该函数的对象的地址.[...](强调和链接我的)
您可以修改对象this点,即*this.例如:
struct X
{
int x;
void foo()
{
this->x =3;
}
};
Run Code Online (Sandbox Code Playgroud)
该方法修改了对象本身,但类似的东西this = new X是非法的.
qwe*_*rtz 27
您可以在以下位置指定对象this点:
*this = XY;
Run Code Online (Sandbox Code Playgroud)
但是你无法分配直接值this:
this = &XY; // Error: Expression is not assignable
Run Code Online (Sandbox Code Playgroud)
das*_*ght 17
很久以前,在第一个C++标准发布之前,一些编译器实现允许您在构造函数中编写以下代码:
this = malloc(sizeof(MyClass)); // <<== No longer allowed
Run Code Online (Sandbox Code Playgroud)
该技术是控制对象类分配的唯一方法.标准禁止这种做法,因为过载operator new已经解决了以前通过作业处理的问题this.
| 归档时间: |
|
| 查看次数: |
13026 次 |
| 最近记录: |