c++ - Why is a VTABLE required when the derived class doesn't override the virtual function? -
c++ - Why is a VTABLE required when the derived class doesn't override the virtual function? -
class base of operations { public: void virtual fn(int i) { cout << "base" << endl; } }; class der : public base{ public: void fn(char i) { cout << "der" << endl; } }; int main() { base* p = new der; char = 5; p->fn(i); cout << sizeof(base); homecoming 0; }
here signature of function fn defined in base
class different signature of function fn()
defined in der
class though function name same. therefore, function defined in der
class hides base
class function fn()
. class der
version of fn cannot called p->fn(i)
call; fine.
my point why sizeof
class base
or der
4
if there no utilize of vtable? requirement of vtable here?
note highly implementation dependent & might vary each compiler.
the requirement presence of vtable
base of operations class meant inheritance , extension, , class deriving might override method.
the 2 classes base of operations , derived might reside in different translation unit , compiler while compiling base of operations class won't know if method overidden or not. so, if finds keyword virtual
generates vtable
.
c++ virtual-functions vtable function-signature
Comments
Post a Comment