开发者

Address of &this instantiated class?

开发者 https://www.devze.com 2023-02-27 11:34 出处:网络
My goal is to write a function tha开发者_如何学运维t returns the address of the instantiated class from which it is called.

My goal is to write a function tha开发者_如何学运维t returns the address of the instantiated class from which it is called.

My initial guess was

return &this

But this does not yield any good results.

Any and all suggestions are much appreciated! Thanks!


Simply return this will return the address of an object from within that object.


this is a pointer. You don't want the address of the this pointer, you want this itself.

Note that under virtual inheritance (virtual base classes, multiple inheritance), the this pointer may not be the same at all times (it would depend on where in the inheritance-graph it is pointing at the specific point in time).

Well defined conversions do exist (dynamic_cast) so no unsolvable problems there, just saying that you should not blindly believe that

MultiplyDerived* d = &someInstance;
Base* b = d;

bool test = ((void*) b) == ((void*) d);

test need not always be true (I think it is even compiler-dependent, i.e. implementation specific what happens when).

0

精彩评论

暂无评论...
验证码 换一张
取 消