c++ - Which function calls are not resolved at compile time in object oriented programming? -
which function calls resolved @ compile time , ones @ runtime? read somewhere not function calls resolved @ compile time dont know which.
virtual function calling mechanisms resolved @ run-time. because, in c++, pointer derived class type-compatible pointer base class. so, call virtual function, actual type of constructed object( or actual under lying object ) base class pointer pointing must known, can resolved @ runtime.
struct foo { virtual void virtualmethod() { cout<< " \n virtualmethod of foo \n"; } void normalmethod() { cout<< " \n normalmethod of foo \n"; } virtual ~foo() {} }; struct bar: public foo { void virtualmethod() { cout<< " \n virtualmethod of bar \n"; } void normalmethod() { cout<< " \n normalmethod of bar \n"; } ~bar() {} }; foo* obj = new bar ; obj->virtualmethod() ;
now, since virtualmethod()
needs called depends on run time type( or actual under lying object ) obj
pointing because obj
can pointed object constructed either new foo
or new bar
. @ run-time, know that obj
constructed objected type returned bar*
, corresponding virtual function of derived class called, if exists. else base class virtual function called.
obj->normalmethod();
this method can resolved @ compile time because it's normal member function.
results: ideone results link
Comments
Post a Comment