OOP question

Started by
1 comment, last by Jman 24 years, 6 months ago
item(base class) -ammo(derived from item) -healthkit(derived from item) -weapon(derived from item) ---throw(derived from weapon which is derived from item) ---melee(derived from weapon which is derived from item) 1) If I have a pointer to an item, do I have to put virtual prototypes of all my derived class functions into the base item class in order to be able to call them, or is there an easier way? 2) How would I setup functions for the throw and melee classes so that I can still call them with a pointer to an item (sort of relates to #1)? If this doesn''t make any sense, please let me know and I''ll try to clarify further. Thanks.
Advertisement
Yes, you do want to specify the functions as virtual in the base class. when you call a virtual funtion it first calls
the function of the derived class. you could then in the derived function call the base classes version of that function. if this doesnt help i can post some source code.

-Item [BASE]
-Weapon [DERIVED]
1) No you don't have to make virtual prototypes in your base classes for functions declared in derivied classes IF you use dynamic_cast and your compiler supports RTTI. dynamic_cast isn't supposed to be very fast (don't really know, I've never used it) so if speed counts you might want to try something else.

2) If the base classes member has a virtual function of the same name as the derivied, simply calling item->member_function will call the member funtion of the class the item pointer accually points to, not item's member function. Hence "virtual". If there is no member in the base class of the same name try using dynamic_cast

I hope I answered your questions.

Edited by - RMack on 2/20/00 9:41:30 AM
- Ryan -

This topic is closed to new replies.

Advertisement