Default arguments and virtual function
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的更多相关文章
- 深入解析pure virtual function call
在本文中,我们将不解释为什么会提示“纯虚拟函数调用”和如何提示“纯虚拟函数调用”,而是详细解释在win32平台的构造函数/析构函数中直接/间接调用纯虚拟函数时程序本身.在开始时,将显示一个经典示例,在 ...
- C# abstract function VS virtual function?
An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...
- why pure virtual function has definition 为什么可以在基类中实现纯虚函数
看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...
- [Python] Problem with Default Arguments
Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...
- Templates and Default Arguments
Default parameters for templates in C++: Like function default arguments, templates can also have de ...
- 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") ...
- pure virtual function call
2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...
- Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法
我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...
- OD: Windows Security Techniques & GS Bypassing via C++ Virtual Function
Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...
随机推荐
- 【前端工具】nodejs+npm+vue 安装(windows)
预备 先看看这几个是干嘛的,相互的关系是啥. nodejs是语言,类比到php. npm是个包管理,类比到composer. vue是个框架,类比到laravel. webpack是个打包工具. 先下 ...
- MySQL高级篇 | MySQL逻辑架构
思维导图 架构逻辑视图 每个虚线框为一层,总共三层. 第一层:连接层,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等. 第二 ...
- dart系列之:dart语言中的函数
目录 简介 函数的参数 main函数 匿名函数 闭包 函数的返回值 总结 简介 函数是所有编程语言都有的内容,不管是面向对象还是面向过程,函数都是非常重要的一部分.dart中的函数和java中的函数有 ...
- 7-7 后缀式求值 (25分)的python实现
exp=input().split() ls=list() def Cal(a,b,i): if i=="+": return a+b elif i=="-": ...
- [jmeter]Jmeter+ant实现接口自动化
1.安装jmeter 和ant &环境变量配置百度去~ 2.jmeter和ant关联 &将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到an ...
- pipeline学习
目录 一.常用语法 二.基础使用 三.使用 Groovy 沙盒 四.参数化构建过程 五.pipeline script from SCM 六.参考 一.常用语法 1.拉取git仓库代码 checkou ...
- 菜鸡的Java笔记 图书馆
图书大厦 开发要求 现在要求模拟一个图书大厦图书管理的程序结构,可以在图书大厦实现某一类图书的上架操作,下架操作,以及关键字模糊查询的操作 注 ...
- VS Code Java 更新 – 全新Gradle for Java插件,更方便的代码操作, 1.0 语言支持发布
大家好,欢迎来到 9 月版的 Visual Studio Code Java 更新.在这篇文章中,我们将分享我们最新的Gradle插件,更加方便的代码操作(Getter/Setter等等),以及最近的 ...
- 15-Transfer Learning
介绍 迁移学习指的就是,假设你手上有一些跟你现在要进行的task没有直接相关的data,那你能不能用这些没有直接相关的data来帮助我们做一些什么事情.比如说:你现在做的是猫跟狗的classifer, ...
- [luogu4318]完全平方数
首先,我们肯定要用到二分答案. 这道题目就是统计第k个μ不是0的数,线性筛显然会炸飞的,但当二分出一个数而统计有多少个小于等于他的合法数时,就可以容斥一下,即:1^2的倍数都不合法,2^2的倍数都不合 ...