Which function calls are resolved at compile time and which ones at runtime? I read somewhere that not all function calls are resolved at compile time i dont know which.
相关问题
- how to define constructor for Python's new Nam
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Keeping track of variable instances
- Why does const allow implicit conversion of refere
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Virtual functions will be resolved at run time. The terms dynamic binding, or late binding is used to portray these functions.
Both the term implies that, the resolution occur during runtime, depending on how the object was instantiated.
Note that, virtual functions are not the only example of late binding. Function pointers can achieve the same effect.
When you use run time binding feature, then the function calls are not resolved at compile time. This is typically Polymorphism using Virtual functions. In such cases, the runtime keeps record of all the virtual function details in a table called VMT (Virtual Method Table) and then in runtime dynamically picks the correct version of the function to be used. The functions which are not virtual are resolved at compile time which is called static binding.
They probably mean virtual functions, which have to go through some mechanism (typically a vtable) to call the right method depending on the current instance.
For example:
Now, within the
Magic()
method there is no way how the compiler can tell what instance will be passed to the method. It could be an instance ofA
and it could be an instance ofB
. The method that has to be called is different in each case. Hence the call is not completely resolved, but goes through some intermediary mechanism, which selects appropriate method.If you mean calls using Polymorphism
compile time : Operator and function overloading. and at runtime : virtual functions.
Virtual function calling mechanisms are resolved at run-time. Because, in C++, a pointer to a derived class is type-compatible with a pointer to its base class. So, to call the virtual function, the actual type of constructed object( or the actual under lying object ) to which base class pointer is pointing to must be known, which can only be resolved at runtime.
Now, since which
virtualMethod()
needs to be called depends on the run time type( or the actual under lying object )obj
is pointing to becauseobj
can be pointed to an object constructed by eithernew foo
ornew bar
. At run-time, we know that thatobj
is constructed from an objected whose type returned isbar*
, corresponding virtual function of the derived class is called, if exists. Else base class virtual function is called.This method can be resolved at compile time itself because it's a normal member function.
Results: ideOne results link