Say I have a Listener class.
I have class A and class M which both implement the listener class.
Class A has 4 M’s.
Each M has 1 A, but not as an A, as a Listener base class. (So downcasted A because M knows nothing about A).
When A gets a message from 1 of them M’s it needs to be able to know which M sent it.
Therefore, every method in the Listener class has a Listener* parameter.
so if I have something like this:
void A::someListenerMethod(Listener* l, MsgEnum msg)
{
if(l == m_mInstance[0])
{
//will this work if the caller was indeed the M instance in question?
}
}
So essentially what I’m asking is, do I need to downcast the M’s to Listeners before comparing them?
I read that sometimes C++ will make a separate object for a sub class for multiple inheritance. There is no multiple inheritance in this case, but I want to make sure this will work.
Thanks
It will work as you expect.
In fact, in order to do the comparison,
m_mInstance[0]will implicitly be cast toListener*in order for the types to match.if (l == (Listener*)(m_mInstance[0])) ...would compile to the same target code. This will typically incur no overhead unless you have multiple inheritance, in which case it might incur a minimal overhead.