B
Bart C
Have just started looking at C++, and tutorials about virtual functions have
thrown up some confusing issues.
These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.
But this is my problem:
Animal X
X=new Cat
I would have thought X was an Animal and not a Cat. This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.
So X carries within it something that identifies the actual class at
runtime?
If Animal was derived from, say Mammal, could X be of that class too?
What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?
If that were possible, it would be pretty powerful, so I'm sure I must be
missing something here.
Thanks,
Bart
thrown up some confusing issues.
These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.
But this is my problem:
Animal X
X=new Cat
I would have thought X was an Animal and not a Cat. This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.
So X carries within it something that identifies the actual class at
runtime?
If Animal was derived from, say Mammal, could X be of that class too?
What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?
If that were possible, it would be pretty powerful, so I'm sure I must be
missing something here.
Thanks,
Bart