+ Reply to Thread
Results 1 to 3 of 3

Thread: What is runtime binding in C++?

  1. #1

    What is runtime binding in C++?

    What is run-time binding in C++?

    How it is different than compile time binding?

    Please provide example.

  2. #2
    Compile time binding some times called binding.

    When we call a method or non virtual member function of a class then it is case of early binding.

    While run-time is called late binding.

    When we call virtual member function of a class then it is called run-time binding. Compiler executes the member function of the class which is currently pointer to by current pointer.

    Code:
    class CBase
    {
    public:
    	void IAmUsingCompileTimeBinding()
    	{
    		char szMessage[256] ;
    		sprintf(szMessage, "CBase::IAmUsingCompileTimeBinding\n") ;
    		OutputDebugString(szMessage) ;
    	}
    
    	virtual void IAmUsingRunTimeBinding()
    	{
    		char szMessage[256] ;
    		sprintf(szMessage, "CBase::IAmUsingRunTimeBinding\n") ;
    		OutputDebugString(szMessage) ;
    	}
    } ;
    
    class CDerived : public CBase
    {
    public:
    	void IAmUsingCompileTimeBinding()
    	{
    		char szMessage[256] ;
    		sprintf(szMessage, "CDerived::IAmUsingCompileTimeBinding\n") ;
    		OutputDebugString(szMessage) ;
    	}
    
    	virtual void IAmUsingRunTimeBinding()
    	{
    		char szMessage[256] ;
    		sprintf(szMessage, "CDerived::IAmUsingRunTimeBinding\n") ;
    		OutputDebugString(szMessage) ;
    	}
    } ;
    
    void UseBinding()
    {
    	CBase objBase ;
    	objBase.IAmUsingCompileTimeBinding() ; //CBase::IAmUsingCompileTimeBinding
    	objBase.IAmUsingRunTimeBinding() ; //CBase::IAmUsingRunTimeBinding
    
    	CDerived objDerived ;
    	objDerived.IAmUsingCompileTimeBinding() ; //CDerived::IAmUsingCompileTimeBinding
    	objDerived.IAmUsingRunTimeBinding() ; //CDerived::IAmUsingRunTimeBinding
    
    	CBase* pBase = &objBase ;
    	pBase->IAmUsingCompileTimeBinding() ; //CBase::IAmUsingCompileTimeBinding
    	pBase->IAmUsingRunTimeBinding() ; //CBase::IAmUsingRunTimeBinding
    
    	//output of below makes difference!
    	pBase = &objDerived ;
    	pBase->IAmUsingCompileTimeBinding() ;//CBase::IAmUsingCompileTimeBinding
    	pBase->IAmUsingRunTimeBinding() ; //CDerived::IAmUsingRunTimeBinding
    }

  3. #3
    Thanks, I get it.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts