   Inheritance - Different derived objects in same list.
  Hi, I am using C++.My situation is that I have CFruit. CApple and COrange inherit from CFruit.egCFruit has:int nZ;bool bIsApple;CApple and COrange inherit from CFruit and also have a few other variables.I want to store objects of CApple and CFruit in a vector. I can then sort any derived object of CFruit by 'z'. I can check the CFruit type is CApple, then access the objects variables.Can someone please give me some method of doing this? Please supply code. And I don't care if it's the wrong or right way, just as long as it works :)   
  Well, the syntax looks like this:class DerivedClassName : BaseClassName {  // normal class internals  // ...};;  I know the code for inheritance, I'm after the code to store objects of CApple and COrange in the same vector.eg Vector contains:AppleOrangeAppleOrangeBut as far as I know, there is no way to do this.Anyone know a way? Please show me.   ;  Something like...//A vector of fruit pointersvector<CFruit*> fruits;//Add an apple and an orangefruits.push_back(new CApple());fruits.push_back(new COrange());//Get the appleCApple* apple = dynamic_cast<CApple*>(fruits.at(0));//Make apple pointer nullapple = 0;//Delete all fruitsfor (vector<CFruit*>::iterator i = fruits.begin(); i!= fruits.end();i++)    delete (*i);//Clear the vector.fruits.clear();Luke.   ;  ThanksThat's what I'm looking for.   ;  Be aware that you normally don't want to do this. dynamic_cast() makes it reasonably safe to try, because it will return null if the object in question is not of the type you claim it is, but you still need to make that check (otherwise boom). As a design principle, if you care about the Apple-ness of your Apples, you shouldn't be storing them with Oranges, because the whole idea of polymorphism is to just .eat() your damn fruit without caring what it is and reap the health benefits. If you want to .impressTeacher(), don't take chances; get your Apple from a reliable Apple source.   ;  It's cause I want to access the variables, not the functions.If I wanted to access functions, I could use polymorphism.But I want to access the variables and save memory by not having a object that is an apple and an orange. In my case an apple OR an orange is needed.Don't worry though, I plan on having a base class variable 'type' to test it. And yeah, I'll take care to not make things buggy.   ;Quote:Original post by utilaeMy situation is that I have CFruit. CApple and COrange inherit from CFruit.egCFruit has:int nZ;bool bIsApple;do you got a good reason for this? sth. like this should be avoided since it breaks the whole point of OOP...   ;Quote:Original post by maximALQuote:Original post by utilaeMy situation is that I have CFruit. CApple and COrange inherit from CFruit.egCFruit has:int nZ;bool bIsApple;do you got a good reason for this? sth. like this should be avoided since it breaks the whole point of OOP...I need to know what derived class the object is before I downcast it into the derived class.And, yeah OOP does not always fit perfectly into what is wanted.;  If you are going down the dynamic_cast() / isApple() route, you might check out the Visitor pattern. It addresses some of these issues but in a more OOP-friendly way.   