0

I know that calling a virtual function from constructor/destructor resolves to an early binding. But I have a question here. Look at the below code

class Base
{
    public:
    virtual void fun()
    {
        cout<<"In Base"<<endl;
    }
};

class Derived : public Base
{
    public:
    Derived(Base obj)
    {
        Base *p;
        p = &obj;
        p->fun();
    }
    void fun()
    {
        cout<<"In Derived"<<endl;
    }
};

int main()
{
    Base b;
    Derived d(b);
}

O/P is : In Base

My doubt here is, that compiler might be using dynamic binding instead of early binding. Please correct me i am wrong but i need a clear explanation of how compiler performs an early binding inside a constructor?

If the method is not a constructor the compiler will convert this statement p->fun in to something like

fetch the VPTR at run time (This vptr will have the derived class VTABLE address)

invoke method at vptr + offset 

hence the right virtual function is invoked.

but since it is a constructor, does compiler stop considering the virtual mechanism and simnply forget about the pointer type and replace the above statement p->fun() to obj.fun() (internally treating it as invoking with the appropriate object)

Because even if you do early binding or late binding the result is same(invoking the local fun() )

Also if it is the local version of the function that is invoked as per the early binding mechanism inside a constructor, then how are we invoking Base version of the function which is not local to the derived (I understand derived contains base sub object but need to know the reason behind calling the base version rather than the local/derived function if the constructor performs an early binding inside a constructor for virtual functions)

Please help

1 Answers1

2

Because you are passing an actual Base to the creator (not a pointer or a reference) what you have there is an actual Base instance - casting the pointer to to Derived* is not valid.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • I think we never land in to a scenario where we accidentally try to access a virtual function down the hierarchy(derived classes) inside a constructor because to do so we need to pass the derived object to the base constructor but by that time when we enter in to base constructor our derived object is not even created. so why we say that we should not call a virtual function inside the constructor so that it might accidentally invoke a function down the hierarchy which is not initialized yet. Could you please provide a small code snippet for what i mentioned if possible.Thanks for the help. – user2906818 Nov 16 '18 at 09:46
  • @user2906818 I have no idea what you are asking but I think you are getting "this" and the parameter mixed up If you pass a constructed Derived object to a constructor by reference of course you can call virtual functions on the parameter but not on this. – John3136 Nov 16 '18 at 10:27
  • Just to add - there is nothing even vaguely tricky in the code in the question. The parameter is a `Base` object not a `Derived` so obviously the base virtual is called. Even the pointer is a red herring. Just try `obj.fun()`. – John3136 Nov 16 '18 at 10:58