C++动态绑定与静态绑定
2015-03-31 22:16
274 查看
静态绑定:编译时绑定,通过对象调用
动态绑定:运行时绑定,通过地址实现
C++的多态性
静态多态性:函数多态性——函数重载
模板多态性——C++模板(类模板、函数模板)
动态多态性:虚函数(只有用地址才能实现动态多态性)
只有采用“指针->函数()”或“引用变量.函数()”的方式调用C++类中的虚函数才会执行动态绑定。对于C++中的非虚函数,因为其不具备动态绑定的特征,所以不管采用什么样的方式调用,都不会执行动态绑定。
C++语言成员函数的调用和绑定方式总结
注:被引用对象所属类 是 指针 或 引用 指向的对象的实际类型;
引用变量所属类、指针变量所属类 是 定义 引用变量、指针变量的类型;
以上两种类型可能相同,也可能不同。
从上表可以看出,执行动态绑定的只有通过地址,即只有通过指针或引用变量才能实现,而且还必须是虚函数。从概念上来说,虚函数机制只有在应用于地址时才有效,因为地址在编译阶段提供的类型信息不完全。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 动态绑定与静态绑定
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CBase
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual int func() const //虚函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout<<"CBase function! ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 100;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CDerive : public CBase
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int func() const //在派生类中重新定义虚函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout<<"CDerive function! ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 200;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CDerive obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase* p1=&obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase& p2=obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase obj2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj1.func(); //静态绑定:调用对象本身(派生类CDerive对象)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1->func(); //动态绑定:调用被引用对象所属类(派生类CDerive)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.func(); //动态绑定:调用被引用对象所属类(派生类CDerive)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.func(); //静态绑定:调用对象本身(基类CBase对象)的函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
动态绑定:运行时绑定,通过地址实现
C++的多态性
静态多态性:函数多态性——函数重载
模板多态性——C++模板(类模板、函数模板)
动态多态性:虚函数(只有用地址才能实现动态多态性)
只有采用“指针->函数()”或“引用变量.函数()”的方式调用C++类中的虚函数才会执行动态绑定。对于C++中的非虚函数,因为其不具备动态绑定的特征,所以不管采用什么样的方式调用,都不会执行动态绑定。
C++语言成员函数的调用和绑定方式总结
代码形式 | [align=center]对于虚函数[/align] | [align=center]对于非虚函数[/align] | ||
作用 | 绑定方式 | 作用 | 绑定方式 | |
类名::函数() | 调用指定类的指定函数 | 静态绑定 | 调用指定类的指定函数 | 静态绑定 |
对象名.函数() | 调用指定对象的指定函数 | 静态绑定 | 调用指定对象的指定函数 | 静态绑定 |
引用变量.函数() | 调用被引用对象所属类的指定函数 | 动态绑定 | 调用引用变量所属类的指定函数 | 静态绑定 |
指针->函数() | 调用被引用对象所属类的指定函数 | 动态绑定 | 调用指针变量所属类的指定函数 | 静态绑定 |
引用变量所属类、指针变量所属类 是 定义 引用变量、指针变量的类型;
以上两种类型可能相同,也可能不同。
从上表可以看出,执行动态绑定的只有通过地址,即只有通过指针或引用变量才能实现,而且还必须是虚函数。从概念上来说,虚函数机制只有在应用于地址时才有效,因为地址在编译阶段提供的类型信息不完全。
例:编写程序观察虚函数和非虚函数的绑定行为:
代码如下:![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 动态绑定与静态绑定
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CBase
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual int func() const //虚函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout<<"CBase function! ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 100;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CDerive : public CBase
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int func() const //在派生类中重新定义虚函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout<<"CDerive function! ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 200;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CDerive obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase* p1=&obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase& p2=obj1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBase obj2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj1.func(); //静态绑定:调用对象本身(派生类CDerive对象)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1->func(); //动态绑定:调用被引用对象所属类(派生类CDerive)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.func(); //动态绑定:调用被引用对象所属类(派生类CDerive)的 func 函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj2.func(); //静态绑定:调用对象本身(基类CBase对象)的函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
相关文章推荐
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- 【转】深入理解C++的动态绑定和静态绑定 & 不要重定义虚函数中的默认参数
- c++动态绑定、静态绑定与java中动态绑定与静态绑定的比较
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- c++动态绑定与静态绑定
- C++的动态绑定和静态绑定
- C++的静态绑定(称静态联编或者早绑定)和动态绑定(动态联编或者晚捆绑) 类继承virtural中用的
- [C++]静态绑定与动态绑定
- 深入理解C++的动态绑定和静态绑定
- C++ 静态绑定和动态绑定
- 深入理解C++的动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定--一个牛人写的文章,浅显易懂
- C++中动态绑定和静态绑定
- 深入理解C++的动态绑定和静态绑定
- C++中的动态绑定与静态绑定