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

后端 未结 5 1130
臣服心动
臣服心动 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:38

    That might be a way to override the this keyword. Lets say that you have a memory pool, full initialized at the start of your program, for instance you know that at any time you can deal with a max of 50 messages, CMessage. You create a pool at the size of 50 * sizeof(CMessage) (what ever this class might be), and CMessage implements the getThis function.

    That way instead of overriding the new keyword you just override the "this", accessing the pool. It can also mean that the object might be defined on different memory spaces, lets say on a SRAM, in boot mode, and then on a SDRAM.

    It might be that the same instance will return different values for getThis through the program in such a situation, on purpose of course, when overriden.

提交回复
热议问题