Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual functions are determine during the compilation?

Tags:

c++

virtual

i tried to look up whether virtual function determine during compilation or while running. while looking i found something as dynamic linking/late binding but i didn't understand if it means that the function itself determine during compilation before the executable or during the executable.

can someone please explain?

like image 751
or.nomore Avatar asked Sep 05 '10 16:09

or.nomore


People also ask

Are virtual functions resolved at compile time?

The idea is that virtual functions are called according to the type of the object instance pointed to or referenced, not according to the type of the pointer or reference. In other words, virtual functions are resolved late, at runtime.

How does the compiler detect virtual functions?

Late binding or Dynamic linkage Therefore compiler determines the type of object at runtime, and then binds the function call. Virtual functions must be members of some class. Virtual functions cannot be static members. They are accessed through object pointers.

What is the function of virtual function?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Which defines a virtual function?

A virtual function is a member function which is declared within a base class and is re-defined (overridden) by a derived class.


2 Answers

For virtual functions resolution is done at runtime. When you have an instance of an object the resolution of which method to call is known only when the program is running because only at runtime you know the exact type of this instance. For non-virtual functions this resolution can be done at compile time because it is known that only this method can be called and there cannot be child classes overriding it. Also that's why virtual method calls are a bit slower (absolutely negligibly but slower than non-virtual method calls). This article explains the concept in more details.

like image 150
Darin Dimitrov Avatar answered Sep 29 '22 12:09

Darin Dimitrov


Usually virtual functions are resolved during runtime. The reasons are obvious: you usually don't know what actual object will be called at the call site.

Base *x;  Derived *y;
Call1(y); 

void Call1(Base *ptr)
{
   ptr->virtual_member();
     // will it be Base::virtual_member or Derived::virtual_member ?
     //runtime resolution needed
}

Such situation, when it's not clear what function will be called at the certain place of code, and only in runtime it's actually determined, is called late binding.

However, in certain cases, you may know the function you're going to call. For example, if you don't call by pointer:

Base x;  Derived y;
Call2(y); 
void Call2(Base ptr)
{
   ptr.virtual_member();
     // It will always be Base::virtual_member even if Derived is passed!
     //No dynamic binding necessary
}
like image 20
P Shved Avatar answered Sep 29 '22 11:09

P Shved