Can 'this' pointer be different than the object's pointer?

后端 未结 5 1193
臣服心动
臣服心动 2021-02-05 11:28

I\'ve recently came across this strange function in some class:

void* getThis() {return this;}

And later in the code it is sometimes used like

5条回答
  •  悲哀的现实
    2021-02-05 11:54

    No. Yes, in limited circumstances.

    This looks like it is something inspired by Smalltalk, in which all objects have a yourself method. There are probably some situations in which this makes code cleaner. As the comments note, this looks like an odd way to even implement this idiom in c++.

    In your specific case, I'd grep for actual usages of the method to see how it is used.

提交回复
热议问题