Predict the output of following C++ program.

 1 #include <iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 virtual void fun ( int x = 0 )
8 {
9 cout << "Base::fun(), x = " << x << endl;
10 }
11 };
12
13 class Derived : public Base
14 {
15 public:
16 virtual void fun ( int x )
17 {
18 cout << "Derived::fun(), x = " << x << endl;
19 }
20 };
21
22
23 int main()
24 {
25 Derived d1;
26 Base *bp = &d1;
27 bp->fun();
28 return 0;
29 }

  Output:

  Derived::fun(), x = 0
    

  If we take a closer look at the output, we observe that fun() of derived class is called and default value of base class fun() is used.
  Default arguments do not participate in signature of functions. So signatures of fun() in base class and derived class are considered same, hence the fun() of base class is overridden. Also, the default value is used at compile time. When compiler sees that an argument is missing in a function call, it substitutes the default value given. Therefore, in the above program, value of x is substituted at compile time, and at run time derived class’s fun() is called.

  Now predict the output of following program.

 1 #include <iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 virtual void fun ( int x = 0)
8 {
9 cout << "Base::fun(), x = " << x << endl;
10 }
11 };
12
13 class Derived : public Base
14 {
15 public:
16 virtual void fun ( int x = 10 ) // NOTE THIS CHANGE
17 {
18 cout << "Derived::fun(), x = " << x << endl;
19 }
20 };
21
22
23 int main()
24 {
25 Derived d1;
26 Base *bp = &d1;
27 bp->fun();
28 return 0;
29 }

  The output of this program is same as the previous program. The reason is same, the default value is substituted at compile time. The fun() is called on bp which is a pointer of Base type. So compiler substitutes 0 (not 10).

  In general, it is a best practice to avoid default values in virtual functions to avoid confusion.

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
  

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  20:18:49

最新文章

  1. UVA 12544 - Beehives O(nm) 无向图最小环
  2. 在CentOS上搭建Storm集群
  3. linux批量复制或删除同命名规则的文件
  4. 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
  5. How to use kingshard building a MySQL cluster
  6. 用JSON-server模拟REST API(一) 安装运行
  7. Media Player(APP)
  8. 15个最新加速 Web 开发的框架和工具
  9. 给Java新手的一些建议----Java知识点归纳(Java基础部分)
  10. Discuz云平台站点信息同步失败,An unknown error occurred. May be DNS Error.
  11. Storm系列(三)Topology提交过程
  12. 一些常用的Intent及intent-filter的信息
  13. MapReduce笔记——技术点汇总
  14. 接口自动化(Python)-利用正则表达式从返回的HTML文本中截取自己想要的值
  15. 关于新版本,iOS10的相关内容,兼容iOS 10 资料整理笔记
  16. rnn_model.fit Incompatible shapes
  17. Hive与HBase表联合使用Join的问题
  18. 一个成功的Git分支模型
  19. JS client(X,Y)、screen(X,Y)、page(X,Y)的区别
  20. linux服务器---安装swat

热门文章

  1. linux&c 进程控制 课后习题
  2. 官宣 .NET RC 2
  3. CTF-Tools 一款CTF古典密码加解密工具
  4. 升级JDK8的坎坷之路
  5. MAC安装vue.js
  6. celery kill task
  7. ORACLE,mysql中替换like的函数
  8. 大一C语言学习笔记(5)---函数篇-定义函数需要了解注意的地方;定义函数的易错点;详细说明函数的每个组合部分的功能及注意事项
  9. 编译使用nginx
  10. Unity Ioc 类型初始值设定项引发异常,The type name or alias SqlServer could not be resolved. Please check your configuration file and verify this type name.