开发者

Which function calls are not resolved at compile time in object oriented programming?

开发者 https://www.devze.com 2023-02-12 07:49 出处:网络
Which function calls are resolved at co开发者_StackOverflowmpile time and which ones at runtime? I read somewhere that not all function calls are resolved at compile time i dont know which.Virtual fun

Which function calls are resolved at co开发者_StackOverflowmpile time and which ones at runtime? I read somewhere that not all function calls are resolved at compile time i dont know which.


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.

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 which virtualMethod() needs to be called depends on the run time type( or the actual under lying object ) obj is pointing to because obj can be pointed to an object constructed by either new foo or new bar. At run-time, we know that that obj is constructed from an objected whose type returned is bar*, corresponding virtual function of the derived class is called, if exists. Else base class virtual function is called.

obj->normalMethod();

This method can be resolved at compile time itself because it's a normal member function.

Results: ideOne results link


If you mean calls using Polymorphism

compile time : Operator and function overloading. and at runtime : virtual functions.


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.

int Add(int nX, int nY)
{
    return nX + nY;
}
int Subtract(int nX, int nY)
{
    return nX - nY;
}

int main()
{
    // Create a function pointer and make it point to the Add function
    int (*pFcn)(int, int) = Add;
    cout << pFcn(5, 3) << endl; // add 5 + 3
    pFcn = Subtract;
    cout<< pFcn(5,3)<<endl // pefrom 5-3
    return 0;
}


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:

class A
{
    public:
        virtual ~A(){}
        virtual void DoStuff() { cout << "A"; }   
};

class B : public A
{
    public:
        virtual void DoStuff() { cout << "B"; }
};

void Magic(A &x)
{
    x.DoStuff();
}

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 of A and it could be an instance of B. 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消