I expect output to be "base foo", but it's "derivde foo".Code:class Base{ public: void foo(){cout<<"base foo"<<endl;} }; class Derived: public Base{ public: void foo(){cout<<"derivde foo"<<endl;} }; void test(Derived* obj){ obj->foo(); } int main(){ Base obj; test((Derived*)&obj); }
My understanding is that obj is a Base type, no matter what type you cast it to. How could a base type refer to a derived type function?



LinkBack URL
About LinkBacks



