Greetings. I have an abstract class, call it C, with two subclasses C1 and C2. In classes C, C1, and C2, there is a virtual method, HasProp1. What I would like to do is to be able to do something like
Sorry. C1 and C2 both also have HasProp1. I have edited the original post accordingly.
As to whether the first option should work, note that CVec is a pointer to C, and that CVec[0] and CVec[1] have a static class of C*, although their dynamic classes are C1 and C2 respectively. Any message sent to an object with static class C* must be able to be understood by C, even if the message would be understood by C1 or C2. Thus, C1::HasProp1 and C2::HasProp1 must have the same type signatures, and the first option is illegal.
Alternatively you can drop third argument instead of throwing an exception
I must note that if you ever need to go down on inheritance tree and choose action based on real type (as opposed to object chosing what to do themselves), you are most likely did your architecture wrong.
If in your case it is impossible to switch array indices on lines 6 and 7, then saving objects as pointers to common type is useless.