c++ - Why is it called dynamic binding? -


we use virtual achieve dynamic binding in cpp i.e. decide @ runtime function has called based on actual object created rather reference or pointer variable.

class {  int a; public:  virtual void show(); };  void a::show() { cout<<a<<endl; }  class b:public {  int b; public:  void show() { cout<<b<<endl; } };  class c:public {  int c; public:  void show() { cout<<c<<endl; } }; 

assume, somefunction(a& aref). can take object of type b or c or a

note: assuming values of data members set

i mean path defined (it can or b or c). isn't run time dependent [like asking user enter age , user enters word or other datatype].

but why called run time binding? compiler make check beforehand if object assigned compatible or no.

is terminology used indicate there no strict association of reference variable particular type of object , decided @ run-time. there more ?

virtual methods create virtual table in object used call methods.

the right method looked @ runtime.

the case it's evident, if you'd have list of base class, contains different kinds of objects:

std::list<a*> mylist = new std::list<a*>(); mylist.push_back(new a()); mylist.push_back(new b()); mylist.push_back(new c());  (a* : mylist) {     a->show(); } 

in little example objects of different types, compiler sees them a object (there's variable of type a calling show()), still right method called.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -