Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Dear sir!

what is difference between early binding and late binding ! May u explain them with example....
Posted

Google is your friend. A quick search just gave me: "Early binding and late binding"[^].
 
Share this answer
 
Comments
Pratik Bhuva 1-Nov-13 6:05am    
5ed For "Google is your friend".
Early binding means that the address to be called when calling a function is statically known at compile time. Late binding means that the address of the function is calculated at runtime.

For a non-oo example something like this might explain it.
#include <iostream>

using namespace std;

int foo() {
	return 100;
}

int bar() {
	return 200;
}

int early_binding(bool b) {
	// Early binding knows the address of the function
	switch(b) {
		case true: return foo();
		case false: return foo();
		default:
			throw exception("well this is unlikely");
	}
}

int late_binding(int (*p)()) {
	return p();
}

int main() {

	cout << "early: " << early_binding(true) << endl;

	// Late binding looks up the actual function to call at runtime
	cout << "late : " << late_binding(true ? foo : bar) << endl;

    return 0;
}


For a OO-example, a virtual class member means that the lookup of the method address has to use the v-table, this leads to late binding (depending on the how the instance is "held");

C#
#include <iostream>

using namespace std;

class base
{
public:
    base() { }
    virtual ~base() { }

    virtual int foo() { return 100; }
};

class a : public base
{
public:
    a() : base() { }
    virtual int foo() { return 200; }
};

int main() {

    base* x = new a();

    // Late binding, v-table is used to lookup correct implementation;
    cout << "Late: " << x->foo() << endl;

    return 0;
}


Hope this helps,
Fredrik
 
Share this answer
 
Comments
CPallini 30-Oct-13 10:09am    
5.However non-OOP example looks 'late binding' in both cases, to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900