When using Polymorphism in C++, what are the important facts to know about it.
Like, for example, when casting from derived
to base
, what should one be aware of?
When is an upcast possible, and when not? When do you need a 'virtual' destructor and when not?
What do you need to be aware of when using pointers from base to derived objects?
There was a discussion in the C++ lounge just now about the sizeof(*this)
with polymorphic types, which inspired me to ask this question.
An "obscure" fact such as this one:
"If you have a Derived that's hidden behind a Base&, then the static type is Base, while the dynamic type is Derived."
Is also the kind of things I am looking for, in this question.
Read "Effective C++" and "More Effective C++". Answering your question here would literally require writing a small book.
精彩评论