I have one class, Class1, and a derived class Class2. If I make a Class1 pointer, and then attribute it to Class2, will this create problems when tying to access members of Class1? Here's pseudo code:
Yes, it's legal, safe and no problems should arise. Moreover, if I'm not mistaken, even static_cast is not needed (you'd be advised to use it when casting to derived classes).
Thanks for your quick reply. I managed to find that my problem was actually that the pointer pointed to where the temporary Class2 was created for my array of Class2's, and not to the actuall class. Thanks!
But this means that the Class1 pointer, even that it points to a data of Class2 it will have the interface of Class1 so if for example Class2 has methodZ and Class1 has methodA your pointer would be able to call only methodA of Class1 or better saying that your pointer will Not be able to call any Class2 member (be it a method or an attribute)
MasterGod wrote:But this means that the Class1 pointer, even that it points to a data of Class2 it will have the interface of Class1 so if for example Class2 has methodZ and Class1 has methodA your pointer would be able to call only methodA of Class1 or better saying that your pointer will Not be able to call any Class2 member (be it a method or an attribute)
...unless it is explicitly cast to Class2*
Just in case - pointer to array of classes has to be deleted with operator "delete[]", as opposed to operator "delete" for classes.