- 浏览: 389183 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
As we all know that we virtual function does not overload an existing function, but rather it reuse the same member method and the right method to be invoked is determined at runtime.
So, here is the problem since we all know that for a given function, you can only define one set of default argument, but the potential number of virtual function (the base virtual function, the derived ones) could be infinite.
Does it mean we cannot redefine the default argument ? With this restriction, there is a predicament. What if you are extending some libraries code, and the libraries code has some default value, while you are taken away the right to provide a more appropriate value for you class. so what is the default argument when virtual function is invovled?
the misunderstood way
/** * file * virtual_function_default_arguments.h * description: * this file demonstrate the use of default argument when the virtual function are considered * */ #include <iostream> using std::cout; using std::cerr; using std::endl; class base { public: virtual int foo(int ival = 1024) { cout << "base::foo() -- ival: " << ival << endl; } }; class derived : public base { public: virtual int foo(int ival =2048) { cout << "derived:foo() -- ival: " << ival << endl; } }
The result turns that it does not work out the way we expected.
As you can see from the output, the default value applied is determined through the the invoker, could it be the pointer, or the reference. So that if you invoke through derived *, then the default argument value of derived version is used. Should you use the base *pointer, the default arguments value of the base classes is choosen. So in nutshell, the value of defaulot argument is not determined at runtime, but rather at compile time.
/** * file * virtual_function_default_arguments.cpp * description: * this file demonstrate the use of default argument when the virtual function are considered * */ #include "virtual_function_default_arguments.h" /** * void virtual_function_default_arguments() * * as you can see from the output, the default value applied is determined through the the invoker, could it be the pointer, or the reference. * so that if you invoke through derived *, then the default argument value of derived version is used. * should you use the base *pointer, the default arguments value of the base classes is choosen. * * so in nutshell, the value of defaulot argument is not determined at runtime, but rather at compile time. */ void virtual_function_default_arguments() { derived * pd = new derived; base *pb = pd; int val = pb->foo(); cout << "main(): val through base: " << val << endl; val = pd->foo(); cout << "main() : val through derived" << val << endl; }
The right way
To use the default value with virtual functio has farreaching meaning, the runtime nature of virtual function and the static nature of default argument does not fit in well with each other. however, if you do want to have default argument with virtual function, instead of simply saying no, you can provide some place holder value which indicate that no value has been passed by the user as shown in the code below.
class base { public: const base_default_value = -1; virtual int bar(int ival = base_default_value) { int real_default_value = 1024; if (ival == base_default_value) { ival = real_default_value; } cout << "base::foo() -- ival: " << ival << endl; } }; class derived : public base { public: virtual int bar(int ival = base_default_value) { int real_default_value = 2048; if (ival == base_default_value) { ival = real_default_value; } cout << "base::foo() -- ival: " << ival << endl; } }
and below shows the code that demon how to use it.
/** * void virtual_function_default_arguments2() * * to use the default value with virtual functio has farreaching meaning, the runtime nature of virtual function and the static nature * of default argument does not fit in well with each other. however, if you do want to have default argument with virtual function * instead of simply saying no, you can provide some place holder value which indicate that no value has been passed by the user * as shown in the code below. */ void virtual_function_default_arguments2() { derived * pd = new derived; base *pb = pd; int val = pb->bar(); cout << "main(): val through base: " << val << endl; val = pd->bar(); cout << "main() : val through derived" << val << endl; }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15284首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 964RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 930Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 923As we all know that virtual des ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1285in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 902class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 956template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1180There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 800the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 553The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1133In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 837A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You can overload the subscript ...
相关推荐
基于ubuntu 16.04.6的 jeos,最小的完整ubuntu系统。 直接解压tar包到ext3、ext4分区,添加grub4dos或grub2引导即可。 注意:可能需要修改解压后的/etc/fstab!
memory vritual to physical.zipmemory vritual to physical.zip
linux Virtual Memory Manager
Dracula for 黑暗主题。 安装 所有说明都可以在上找到。 团队 此主题由以下人员和一堆维护。 贡献 如果您想为这个主题做贡献,请阅读。 执照
gitbub上面不支持乱码,此jar包支持,如果此资源不能满足,请直接下载https://download.csdn.net/download/luozhuwang/11958429 此链接,已经包含Jmeter 3.3 可以直接使用
1、请到官网下载hipchat server 虚拟机模板 https://hipchat-server-stable.s3.amazonaws.com/HipChat.ova 2、Vritual box 导入虚拟机 3、参照 hipChat破解手册.doc 的破解步骤进行破解
您可以使用SQL搜索文件,如下所示: select ctime, atime, mtime, uid, gid, size / ( 1024 * 1024 ) as ' size (MB) ' , path || ' / ' || name as file from fs where path match ' /usr/lib, /var/log ' and (...
引言 今年, 疫情 并没有影响到各种面经的正常出现,可谓是...然后,再由 render() 函数转为 Vritual DOM (以下统称 VNode )。而 render() 函数转为 VNode 的过程,是由 createElement() 函数完成的。 因此,本次文章
Bamboo Virtual ListView Control --Elmer(elmer728@126.com) 2007.06 ====================================================几个优点: 1.最明显的两个优点就是美观和高效。界面美观,定制性高;...
库层LIBRARY: (SQlite、opengl|es,surfece Manager) 和 Android Delvik Vritual Machine Application Framenwork: Activity Manager \ WiFi Manager\ Location Manager…. Applicaton : 游戏 、应用 2. ...