什么是 C++ 中的早期(静态)和晚期(动态)绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18035293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is early (static) and late (dynamic) binding in C++?
提问by Slazer
How does early and late binding look like in C++? Can you give example?
C++ 中的早期和晚期绑定是什么样的?你能举个例子吗?
I read that function overloading is early binding and virtual functions is late binding. I readthat "early (or static) binding refers to compile time binding and late (or dynamic) binding refers to runtime binding".
我读到函数重载是早期绑定,虚函数是后期绑定。我读到“早期(或静态)绑定是指编译时绑定,后期(或动态)绑定是指运行时绑定”。
采纳答案by Matthieu M.
You read right. The basic example can be given with:
你没看错。可以给出基本示例:
using FuncType = int(*)(int,int); // pointer to a function
// taking 2 ints and returning one.
int add(int a, int b) { return a + b; }
int substract(int a, int b) { return a - b; }
Static binding is when binding is known at compile time:
静态绑定是在编译时已知绑定时:
int main() {
std::cout << add(4, 5) << "\n";
}
leaves no room for a dynamic change of the operation, and thus is statically bound.
没有为操作的动态变化留下空间,因此是静态绑定的。
int main() {
char op = 0;
std::cin >> op;
FuncType const function = op == '+' ? &add : &substract;
std::cout << function(4, 5) << "\n";
}
whereas here, depending on the input, one gets either 9 or -1. This is dynamically bound.
而在这里,根据输入,一个人会得到 9 或 -1。这是动态绑定的。
Furthermore, in object oriented languages, virtual
functions can be used to dynamically bind something. A more verbose example could thus be:
此外,在面向对象的语言中,virtual
函数可用于动态绑定某些东西。因此,更详细的示例可能是:
struct Function {
virtual ~Function() {}
virtual int doit(int, int) const = 0;
};
struct Add: Function {
virtual int doit(int a, int b) const override { return a + b; }
};
struct Substract: Function {
virtual int doit(int a, int b) const override { return a - b; }
};
int main() {
char op = 0;
std::cin >> op;
std::unique_ptr<Function> func =
op == '+' ? std::unique_ptr<Function>{new Add{}}
: std::unique_ptr<Function>{new Substract{}};
std::cout << func->doit(4, 5) << "\n";
}
which is semantically equivalent to the previous example... but introduces late binding by virtual
function which is common in object-oriented programming.
这在语义上等同于前面的示例......但引入了virtual
面向对象编程中常见的函数后期绑定。
回答by duffymo
These are true of all object-oriented languages, not just C++.
所有面向对象的语言都是如此,而不仅仅是 C++。
Static, compile time binding is easy. There's no polymorphism involved. You know the type of the object when you write and compile and run the code. Sometimes a dog is just a dog.
静态的编译时绑定很容易。不涉及多态性。当您编写、编译和运行代码时,您就知道对象的类型。有时狗只是一只狗。
Dynamic, runtime binding is where polymorphism comes from.
动态的运行时绑定是多态性的来源。
If you have a reference that's of parent type at compile type, you can assign a child type to it at runtime. The behavior of the reference will magically change to the appropriate type at runtime. A virtual table lookup will be done to let the runtime figure out what the dynamic type is.
如果在编译类型中有一个父类型的引用,则可以在运行时为其分配子类型。引用的行为将在运行时神奇地更改为适当的类型。将执行虚拟表查找以让运行时确定动态类型是什么。
回答by rahul kumar
Static binding: if the function calling is known at compile time then it is known as static binding. in static binding type of object matter accordingly suitable function is called. as shown in the example below obj_a.fun() here obj_a is of class A that's why fun() of class is called.
静态绑定:如果函数调用在编译时已知,则称为静态绑定。在对象的静态绑定类型中,相应地调用了合适的函数。如下面的示例所示 obj_a.fun() 此处 obj_a 属于 A 类,这就是调用类的 fun() 的原因。
#include<iostream>
using namespace std;
class A{
public:
void fun()
{cout<<"hello world\n";}
};
class B:public A{
public:
void show()
{cout<<"how are you ?\n";}
};
int main()
{
A obj_a; //creating objects
B obj_b;
obj_a.fun(); //it is known at compile time that it has to call fun()
obj_b.show(); //it is known at compile time that it has to call show()
return 0;
}
dynamic binding: if the function calling is known at run time then it is known as dynamic binding. we achieve late binding by using virtual keyword.as base pointer can hold address of child pointers also. so in this content of the pointer matter.whether pointer is holding the address of base class or child class
动态绑定:如果函数调用在运行时已知,则称为动态绑定。我们通过使用 virtual 关键字实现后期绑定。因为基指针也可以保存子指针的地址。所以在指针的这个内容中,指针是否持有基类或子类的地址
#include<iostream>
using namespace std;
class car{
public:
virtual void speed()
{
cout<<"ordinary car: Maximum speed limit is 200kmph\n";
}
};
class sports_car:public car{
void speed()
{
cout<<"Sports car: Maximum speed is 300kmph\n";
}
};
int main()
{
car *ptr , car_obj; // creating object and pointer to car
sports_car sport_car_obj; //creating object of sport_car
ptr = &sport_car_obj; // assigining address of sport_car to pointer
//object of car
ptr->speed(); // it will call function of sports_car
return 0;
}
if we remove virtual keyword from car class then it will call function of car class. but now it is calling speed function of sport_car class. this is dynamic binding as during function calling the content of the pointer matter not the type of pointer. as ptr is of type car but holding the address of sport_car that's why sport_car speed() is called.
如果我们从 car 类中删除 virtual 关键字,那么它将调用 car 类的函数。但现在它正在调用sport_car 类的速度函数。这是动态绑定,因为在函数调用期间,指针的内容与指针的类型无关。因为 ptr 是 car 类型,但保存了 sport_car 的地址,这就是为什么要调用 sport_car speed() 的原因。