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注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...
随机推荐
- 17.彻底解决Jmap在mac版本无法使用的问题
彻底解决Jmap在mac版本无法使用的问题 看了网上很多帖子,都说一半,说的都是大家说过的,根本没有解决问题.说jdk8不行,换成jdk9或者jdk11,我都试了,还是不行,最后说是mac的问题.换成 ...
- HTTP 缓存终极指南
TL;DR 错误的缓存策略是如何抵消你所做的性能优化工作的. 缓存存在于客户端并且通过chrome或者其他抓包工具查看其状态信息. 客户端通过header中的各个字段做缓存的过期判断. 代理服务器上也 ...
- IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法
出现这种错误可以在pom.xml里配置, 找到<build>标签在下面<plugins>标签上面加上<defaultGoal>compile</default ...
- requests之代理的使用
import requests # 访问url url = 'http://www.baidu.com/s?' # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 ...
- java 获得 微信 UserId
.... public String cs() throws Exception{ /*访问页面,服务器会得到 code(request.getParameter("code")) ...
- jpg与jpeg的区别在哪
JPG文件的优点是体积小巧,并且兼容性好,因为大部分的程序都能读取这种文件,这是因为JPG格式不仅是一个工业标准格式,而且更是web的标准文件格式.JPG文件如此拥有如此便利的条件,难怪得到了业余玩家 ...
- [atARC116F]Deque Game
假设两个操作者分别为$A$和$B$,其中$A$希望最大.$B$希望最小 (并不默认$A$为整局游戏的先手,仅是最终的结果考虑$A$为先手时) 记第$i$个队列第$j$个元素为$a_{i,j}$(其中$ ...
- [atARC094D]Worst Case
首先,容易证明满足条件的$ip_{i}$必然是一个前缀 将其看成一张二分图,$i$向满足$ip_{i}<xy$的$p_{i}$连边,即找到一个前缀满足其有完美匹配 二分枚举前缀长度$k$,根据h ...
- [atARC096F]Sweet Alchemy
给定一棵有根树,记$f_{i}$表示$i$的父亲,每一个点有一个代价$c_{i}$ 给定常数$D$和$X$,再给每个点赋一个权值$v_{i}$($v_{i}\ge 0$),满足以下条件下最大化$\su ...
- [noi1779]D
先离散,然后将黑的看成1,白的看成-1,对整个序列差分,所有区间建为$(l,r+1)$的无向边,并标上-1和1,每一个点的前缀和即为该点的值 考虑什么情况下能够使得所有点都是0:当且仅当每一个点的度数 ...