Inline functions cannot be virtual functions because the virtual table mechanism requires a real function address. After an inline function is expanded, it is not a function, but a simple one.Code(Most C ++ object models use virtual tables to implement polymorphism, which is supported by this standard.) Some inline functions may not be able to expand inline and compile them into functions.
Class foo
{
Public:
Virtual void vfunc () const;
PRIVATE:
};
Class child: Public foo
{
Public:
Virtual void vfunc () const;
};
Inline
Void FOO: vfunc () const
{Cout <"class foo" <Endl ;}
Inline
Void child: vfunc () const
{
Cout <"class child" <Endl;
}
Int main ()
{
Foo * PCH = new child;
PCH-> vfunc ();
Return 0;
}
Output is child! Even if the virtual function is an inline function, it is still correctly bound dynamically!
The reason is: even if a virtual function is declared as an inline function, the compiler will not expand such a function in inline mode, but process it as a normal function.
Constructors cannot be virtual functions. Virtual functions are implemented by using the same function name in the inherited class. The constructor does not need or can make you rtti! (The subclass name cannot be the same as the parent class name)