tags:

views:

88

answers:

3

Hello Everyone !

I have the following pair of functions:

void RegisterSink( ISink & Sink )
void UnregisterSink( ISink & Sink )

Where ISink is an abstract base class. Internally, I would like to store pointers to the sinks in an std::set. When a sink is unregistered, i simply search for the pointer in my set, and remove it. My question is, is there any way, that taking the adress of the parameter Sink would yield different results, although the same object was passed as a parameter. I know, that pointers can change when casting in certain multiple inheritance szenarios, but what about this situation?

Thanks in advance!

+8  A: 

As you've said, pointer addresses may change when casting between virtual classes with multiple inheritance. However, in your case the static type is always the same: ISink so comparing two points of this static type is guaranteed to be safe, and yield reproducible results.

Konrad Rudolph
+2  A: 

Offset adjustments are made in case of multiple inheritances. IFAIK, they must lead to pointers comparing equal and sortable with std::less<> excepted if your object inherit several times from ISink while ISink is not a virtual base class in all cases.

AProgrammer
+1  A: 

In case of multiple inheritance the meaning of "same object" sometimes is not obvious. For example, if ISink is present twice in the list of base classes and wasn't inherited with "virtual", such situation is possible:

class A {};
class B:public A {};
class C:public A {};
class D:public B,public C {};
...
void f(A *a);
...
{
    D d;
    f(static_cast<B*>(&d));
    f(static_cast<C*>(&d));
}

In this case f will get two different addresses. Whether is's same object or not probably depends on context. If you want to treat them as the same object, dynamic_casting to void* may help - it castes to most derived class (something virtual in A is needed, of course)

maxim1000