How can you assign a value to the pointer 'this' in C++

后端 未结 6 2163
北荒
北荒 2020-12-08 21:03

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

6条回答
  •  攒了一身酷
    2020-12-08 21:58

    Long ago, before the first C++ standard has been published, some compiler implementations allowed you to write the following code inside a constructor:

    this = malloc(sizeof(MyClass)); // <<== No longer allowed
    

    The technique served as the only way to control allocation of class of objects. This practice has been prohibited by the standard, because overloading of the operator new has solved the problem that used to be tackled by assignments to this.

提交回复
热议问题