Upcasting and downcasting class pointers.

Started by
1 comment, last by hellz 20 years, 8 months ago
Hey all. I''ve been going through a C++ for a while now, and I''m at the point where I''m very confident with most constructs of the language. However, to make absolutely certain of this, I''ve been coding lots of little "meaningless" programs; it seems to be doing the trick. Now, I''ve just come across upcasting and downcasting of class pointers in the book I''m going through. So, again, I thought I''d try my own example (just to see if I know when to use this sort of theory). I''ve come up with the following test scenario: 1.) Make a class called Garage, which should be used to store MAXSPACES vehicles. 2.) Have a generic Vehicle class, of which several derived classes can be made; classes Car and Bike, just to keep it simple. 3.) Have an array of pointers to type Vehicle in class Garage; these would be dynamically allocated to Vehicles classes by the Garage constructor. The idea was to produce this set of classes, and fiddle around with up and downcasting pointers (being as Vehicle is a generic type, and I wouldn''t know the vehicles that would be stored in Garage until run-time). Am I on the right lines with this, or can someone think of a better scenario to have a bash at? Thanks. --hellz
Advertisement
Do something with arrays. Make sure you know why an array of derived classes is not an array of parent classes, and vice versa.
Nice idea, and it ties in with sort of what I was saying (although your scenario attacks it more head on).

I''ll have a go at that mate, thanks.

--hellz

This topic is closed to new replies.

Advertisement