oops的高级概念 [英] advanced concepts of oops

查看:70
本文介绍了oops的高级概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生!



早期绑定和后期绑定有什么区别!你可以用例子来解释它们。

Dear sir!

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

推荐答案

谷歌是你的朋友。快速搜索给了我:早期绑定和后期绑定 [ ^ ]。
Google is your friend. A quick search just gave me: "Early binding and late binding"[^].


早期绑定意味着在编译时静态地知道调用函数时要调用的地址。后期绑定意味着函数的地址是在运行时计算的。



对于非oo示例,这样的事情可能会解释它。

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;
}





对于OO示例,虚拟类成员意味着查找方法地址必须使用v-table,这导致后期绑定(取决于实例是如何保持);





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");

#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;
}





希望这会有所帮助,

Fredrik



Hope this helps,
Fredrik


这篇关于oops的高级概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆