Click here to Skip to main content
15,741,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a class hierarchy of multiple interface inheritance


IInterface1: public IBase
IInterface2: public IBase

CClass: public IInterface1, IInterface2

(all these interfaces are pure virtual)

Now, if I have a function that takes objects of both types:
void MyFunc( IInterface1* pObj1, IInterface2* pObj2 );

how can I detect if pObj1 and pObj2 are the same object? I can't simply cast down to IBase*, because of the multiple inheritance.

So far, the best answer I've found is to have a GetUID() method in IBase, and make each object generate a UID at creation time. Is there a better solution?

[update] Simply comparing the pointers won't work - even if cast down to base, since pObj1 has Interface1's implementation of IBase, and pObj2 has Interface2's implementation of IBase (due to the way c++ vtables work)
Updated 27-Jan-10 23:33pm

1 solution

Comparing pointers doesn't work because the two bases have different offset, and casting to CClass may not be what you mean (may be it is not the only class in your system actually having this "property").

The proper way is
dynamic_cast<void*>(pobj1) == dynamic_cast<void*>(pobj2) 
that, by definition, will compare the addresses of the most external part of the objects whatever the runtime type is and whatever the compared bases are.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900