Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12770
  • 博文数量: 8
  • 博客积分: 1505
  • 博客等级: 上尉
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-01 23:31
文章分类

全部博文(8)

文章存档

2011年(2)

2010年(6)

我的朋友

分类: C/C++

2011-09-03 15:40:38

ISO/IEC 14482 (2003-10-15)
8.3.6 Default arguments
10 A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides. [Example:
struct A {
virtual void f (int a = 7);
};
struct B : public A {
void f (int a);
};
void m()
{
B* pb = new B;
A* pa = pb;
pa->f(); //OK,calls pa->B::f(7)
pb->f(); //error:wrong number of arguments for B::f()
-end example]
用gcc进行实验,发现确实如此。
为了完整性,
第二行改为:virtual void f (int a);
第五行改为:void f (int a = 7);
进行实验发现仍然有错误。
理解:
联想到目前所有的编译器都通过虚函数表来实现虚函数机制,而虚函数表中存储的是虚函数的入口地址。
虚函数的作用是实现动态联编,也就是在程序的运行阶段动态地选择合适的成员函数。
由此可推断,虚函数的参数在编译阶段已经根据the static type of the pointer or reference denoting the object确定下来了,然后在运行阶段决定调用哪个虚函数。
阅读(1049) | 评论(0) | 转发(0) |
0

上一篇:欧拉公式

下一篇:目录访问权限

给主人留下些什么吧!~~