   How to have a different struct types in the same list, using inheritance?
  Hi, I am using C++ and D3DXSprite. I have made a base struct that has some base variables. I have two structs that inherit those variables.What I want to do, is to have a vector full of SPRITEX structs, which are either SPRITEIMAGE or SPRITETEXT, so I can access both struct types from the same vector list (eg vector <SPRITEX> m_lstSpritexList;).//struct inheritance code://spritex base structstruct SPRITEX {int m_nZ;//z (1 close, 5 far)bool m_bText;RECT m_recDest;//destination positionbool operator<(const SPRITEX &rhs) const {return m_nZ>rhs.m_nZ;}//operator to sort vertex struct};//spritex inherited struct that is only for imagesstruct SPRITEIMAGE: public SPRITEX{string m_sTextureName;string m_sTileName;float m_fRotation;//rotation in degrees};//spritex inherited struct that is only for textstruct SPRITETEXT: public SPRITEX {string m_sText;int m_nAlignment;//left/center/rightbool m_bWraptext;DWORD m_dwColor;//text colorvector<LOADEDFONT*>::iterator m_itFont;//iterator pointing to a font};But when I loop through my vector list, I find that I cannot access the variables of my derived structs. See the ???????//loop through list of sprites and text to rendervector<SPRITEX>::iterator itSprite=m_lstSpritexList.begin();while(itSprite!=m_lstSpritexList.end()){itSprite->???????????????? //does not contain variables from derived structs//only base variables are there//for those concerned, I have put the ??? there to make a point, that's not an error}So, any way to do this????   
  Use a list of pointers to SPRITEX.   ;  Yeah, you need to use a list of pointers, and dynamic_cast to the derived typesinorder to gain their memeber variables.This means you need some identifier in the base class (or some really fancy templet tricks) to let you knowhow to cast your variables.ALso, dynamic_cast returns a null if it couldn't cast, so you can try casting to everything, but that would be a waste.What you should probably think of doing is determining everything you need, and make that in the base class. Includinga virtual Render(); function.  That way you will never need access to the optional information inside the derived SPRITETEXT and SPRITEIMAGE.Thus, you will not have to deal with casting away from the SPRITEX * stored in your container.   ;  Can you give me an example of dynamic_cast?I'm not sure where to use it.-Do I use it once, when I add my object to the list?-Or Do I use it whenever I read my iterator?[Edited by - utilae on June 4, 2006 6:30:25 PM];  You could use something like this which does not require any cast.enum SPRITE_TYPE{IMAGE,TEXT};//spritex base structstruct SPRITEX {SPRITE_TYPE type;//added to baseint m_nZ;//z (1 close, 5 far)bool m_bText;RECT m_recDest;//destination positionbool operator<(const SPRITEX &rhs) const {return m_nZ>rhs.m_nZ;}//operator to sort vertex struct};union SPRITE{SPRITE_TYPE type;SPRITEIMAGE image;SPRITETEXT text;};//loop through list(list or vector? what function do you require from the container?) of sprites and text to rendervector<SPRITE*>::iterator itSprite=m_lstSpritexList.begin();while(itSprite!=m_lstSpritexList.end()){switch ( (*itSprite)->type ){case IMAGE: (*itSprite)->image.m_fRotation = 45.0f;;break;case TEXT: (*itSprite)->text.m_bWraptext = false;break;default break;}};  Well, you could, put honestly, please don't.  Type switching is brittle and error prone.  The accepted solution in modern C++ to problems like this is to use virtual functions.  It's harder to make an error, the compiler does most of the work for you and it's more maintainable.Σnigma   ;  ok change the lines forif ( (*itSprite)->type == IMAGE)(*itSprite)->image.m_fRotation = 45.0f;else if( (*itSprite)->type == TEXT)(*itSprite)->text.m_bWraptext = false;:)   ;  That's still type switching.Σnigma   ;  Ah, I'll just do this://spritex base structstruct SPRITEX {int m_nZ;//z (1 close, 5 far)bool m_bText;RECT m_recDest;//destination positionSPRITEIMAGE *m_SPImage;SPRITETEXT *m_SPText;bool operator<(const SPRITEX &rhs) const {return m_nZ>rhs.m_nZ;}//operator to sort vertex struct};//spritex component struct that is only for imagesstruct SPRITEIMAGE{string m_sTextureName;string m_sTileName;float m_fRotation;//rotation in degrees};//spritex component struct that is only for textstruct SPRITETEXT{string m_sText;int m_nAlignment;//left/center/rightbool m_bWraptext;DWORD m_dwColor;//text colorvector<LOADEDFONT*>::iterator m_itFont;//iterator pointing to a font};That way I can just sort the SPRITEX's then access the subclasses easily.[Edited by - utilae on June 4, 2006 9:28:45 PM]