Archived

This topic is now archived and is closed to further replies.

Inherited classes and their respective pointers??

This topic is 5535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let''s say I have a struct that acts as a template for some game object, let this be a chassis template: struct Tchassis { char * name; int somedata; } The char * name is the name of the template. here''s my array of templates: Tchassis Tchassis_array[numTchassis]; Here''s a function to get a pointer to one of them, more or less: Tchassis * GetChassisTemplate(char * name) { for(int i = 0; i < numTchassis; i++) { if(!strcmp(Tchassis_array.name, name)) return &Tchassis_array[i]; //or something, i forgot } return 0; } (Good job on reading this far) Now This would work, but let''s say I have some other templates, Tengine, and Tbody. I don''t really want to write that function over and over for each template, so I tried to do this: struct Tbase { char * name; } struct Tchassis : Tbase { int somedata; } //etc... Tbase * GetBaseTemplate(Tbase * array, char * name) { for(int i = 0; i < numTchassis; i++) { if(!strcmp(array[i].name, name)) return &array[i]; //or something, i forgot syntax } return 0; } So, the only problem with this is the unability to convert a Tbase* to a Tchassis or some other inherited class. I can go from inherited up, but not down. I wonder what way I could fix this to let me use just one function and specify the array. Thanks greatly for your time and patience. Please respond, even if you''re telling me it can''t be done! return Neff; //Hi-HO!

Share this post


Link to post
Share on other sites
Thanks for your reply, I will look into it! No runtime costs for me, as I will be accessing this only once, or very rarely at least.

Share this post


Link to post
Share on other sites
quote:
Original post by Neff
*hork* I spoke too soon I think. Every time I access the pointer, would it have to up/down cast? If not then good...


Derived* pDerived = dynamic_cast<Derived*>( pBase );

Then you can do whatever you want with your pDerived pointer. If pBase was not really pointing to a Derived object, then pDerived will be NULL (remember to check for it). dynamic_cast is much more expensive than a virtual function call, consider placing a virtual function (possibly pure virtual) in the base class, and implementing it appropriately in the derived classes. That way no cast is necessary.

If you find yourself repeatedly downcasting, it is a sign that your class hierarchy was poorly designed.

Also, consider using the standard C++ string class (std::string), rather than using char* C strings. It''ll make your life easier.

Finally, using a standard C++ associative array (std::map) might completely solve your problem (finding an object by name in a table), since that''s what that class has been designed for.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites