Does RTTI work with final?
I am learning C ++ 11 through the C ++ 11 FAQ from Stroustrup. I have a question.
If the virtual function is defined as final in the class, then RTTI (dynamic_cast and typeid) is still working on its derived class?
@MSalters: My intention was, let's say:
struct A {
virtual void f() final; // only one virtual function, but final
};
struct B : A {
};
A* pa = new B;
B* pb = dynamic_cast<B*>(pa); // would this work? I guess it applies to typeid as well.
+3
source to share
1 answer
Declaring a virtual function final
in the base class prevents it from being overridden (10.3 / 4). It is still inherited because all members are inherited (modulo chapter 12, special member functions). Hence the derived class is polymorphic (10.3 / 1) and runs RTTI.
(I am assuming you are not going to make your own dtor final
. It doesn't work.)
+3
source to share