8/17 In C++ (and Java), virtual functions are "turned off" in the
constructors and destructors, right? Since the child class
isn't available.
\_ you can definitely have virtual destructors (in C++).
It is strongly recommended.
\_ I meant calling a VF from a ctor or dtor. --OP
\_ even if it were legal, that is really bad style. A parent
class having knowledge of children classes? Bad bad bad.
\_ Um, the point of a VF is that you don't have to know the
exact behavior. Fortunately, this isn't allowed anyway.
\_ They're not "turned off"--that is, the compiler doesn't prevent you
from calling them, but you shouldn't expect them to work. When
you're constructing, you start at the base constructor and work your
way down the inheritance chain. So if you try to call a virtual
function in that process, the full type hasn't been constructed and
you can't expect anything predictable. Similarly in the destructor.
This is a reason many people on the C++ community are asking for
"post-construction" and "pre-destruction" functions.
\_ This is incorrect. It is perfectly predictable; in a constructor
you are guaranteed to have the virtual functions defined in that
class or any base class. --pld
lazy emacs user was here |