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

Default arguments and virtual function的更多相关文章

  1. 深入解析pure virtual function call

    在本文中,我们将不解释为什么会提示“纯虚拟函数调用”和如何提示“纯虚拟函数调用”,而是详细解释在win32平台的构造函数/析构函数中直接/间接调用纯虚拟函数时程序本身.在开始时,将显示一个经典示例,在 ...

  2. C# abstract function VS virtual function?

    An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...

  3. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  4. [Python] Problem with Default Arguments

    Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...

  5. Templates and Default Arguments

    Default parameters for templates in C++: Like function default arguments, templates can also have de ...

  6. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

  7. pure virtual function call

    2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...

  8. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

  9. OD: Windows Security Techniques & GS Bypassing via C++ Virtual Function

    Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...

随机推荐

  1. Window黑客编程之资源释放技术

    前言 今天说一下写病毒木马会广泛使用的一种技术--资源释放技术.为什么我们在写木马时会使用到资源释放技术呢?这是因为它可以使我们写的程序变得简洁.如果程序需要额外加载一些DLL文件或者文本文件,我们可 ...

  2. Vuex状态管理——任意组件间通信

    核心概念 在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信. 每一个 Vuex 应用的 ...

  3. Java学习(十八)

    学习了Web中的单位. 像素是网页中最常用到的单位,一个像素是屏幕中的一个小点. 不同显示器一个像素的大小也不同,像素越小,显示效果越好. 也可以用百分比的方式: <!DOCTYPE html& ...

  4. Hadoop HA集群 与 开发环境部署

    每一次 Hadoop 生态的更新都是如此令人激动 像是 hadoop3x 精简了内核,spark3 在调用 R 语言的 UDF 方面,速度提升了 40 倍 所以该文章肯定得配备上最新的生态 hadoo ...

  5. C++内存管理剖析

    C++内存管理 C++中有四种内存分配.释放方式: 最高级的是std::allocator,对应的释放方式是std::deallocate,可以自由设计来搭配任何容器:new/delete系列是C++ ...

  6. 生产服务GC调优实践基本流程总结

    Photo by Pixabay from Pexels 本文作者:夜色微光 - 博客园 (cnblogs.com) 前言 对Java虚拟机进行性能调优是一个非常宽泛的话题,在实践上也是非常棘手的过程 ...

  7. .NET Core中的鉴权授权正确方式(.NET5)

    一.简介 前后端分离的站点一般都会用jwt或IdentityServer4之类的生成token的方式进行登录鉴权.这里要说的是小项目没有做前后端分离的时站点登录授权的正确方式. 一.传统的授权方式 这 ...

  8. Spring Boot的前世今生以及它和Spring Cloud的关系详解。

    要了解Spring Boot的发展背景,还得从2004年Spring Framework1.0版本发布开始说起,不过大家都是从开始学习Java就使用Spring Framework了,所以就不做过多展 ...

  9. html+css第九篇

    热区: <img src="img/login.gif" alt="" border="0" usemap="#Map&qu ...

  10. Python实战:截图识别文字,过万使用量版本!(附源码!!)

    前人栽树后人乘凉,以不造轮子为由 使用百度的图片识字功能,实现了一个上万次使用量的脚本. 系统:win10 Python版本:python3.8.6 pycharm版本:pycharm 2021.1. ...