- 浏览: 399972 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1013RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 772It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 964As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 773There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 825we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 980As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 976As we all know that virtual des ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1331in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 987template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 856the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 589The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1171In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 875A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1188You can overload the subscript ...
相关推荐
基于ubuntu 16.04.6的 jeos,最小的完整ubuntu系统。 直接解压tar包到ext3、ext4分区,添加grub4dos或grub2引导即可。 注意:可能需要修改解压后的/etc/fstab!
内存虚拟化到物理内存的转换是现代计算机系统中一个至关重要的技术,特别是在多任务和多用户的操作系统中。这个主题涉及到如何高效地管理有限的物理内存资源,为多个虚拟地址空间提供服务。以下是对这一概念的详细...
VirtualBox号称是最强的免费虚拟机软件,它不仅具有丰富的特色,而且性能也很优异!它简单易用,可虚拟的系统包括Windows(从Windows 3.1到Windows10、Windows Server 2012,所有的Windows系统都支持)、Mac OS X、...
《VirtualBox 5.2.44-139111-Win:虚拟化技术的卓越工具》 VirtualBox,作为一个开源且跨平台的虚拟化软件,是全球许多企业和个人用户的重要工具,用于在一台物理计算机上运行多个操作系统。这款软件由德国公司...
《深色主题:Visual Studio Code中的Dracula主题详解》 Visual Studio Code,简称VS Code,是微软开发的一款轻量级且强大的源代码编辑器,它支持多种编程语言,并且具有丰富的扩展功能。对于长时间编程的开发者来说...
chown virtual:vritual /var/ftp/share -R ``` 4. **设置目录权限**: ```bash chmod -R 755 /var/ftp/share ``` #### 三、虚拟用户账号与数据库配置 接下来需要创建虚拟用户的登录文件和数据库文件。 1. *...
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 (...
Allegro是一款广泛应用于电子设计自动化(EDA)领域的PCB设计软件,它能够帮助工程师完成电路板的设计、分析和制造等工作。在使用Allegro进行PCB设计时,软件提供了丰富的参数设置选项,其中Display面板参数对于控制...
在本文中,我们将深入探讨如何在Qt5框架中实现虚拟键盘功能,并且特别关注如何让虚拟键盘支持中文拼音输入、大小调整以及鼠标拖动。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备。...
Python虚拟桌面访问器Python模块提供对通过访问的大多数设置的编程访问。 包括切换虚拟桌面,在虚拟桌面之间移动窗口,固定窗口以及列出桌面上的窗口。 最初基于 。安装pip install pyvda用法示例from pyvda import ...
在验证 RP4VM 且测试保护虚拟机时,需要登录 vCenter,主页,可以查看到生成的插件 RecoverPoint for VMs,选择要保护的虚拟机,右键,所有 RecoverPoint for vritual Machines 操作为这个虚拟机创建一个新的一致性...
引言 今年, 疫情 并没有影响到各种面经的正常出现,可谓是...然后,再由 render() 函数转为 Vritual DOM (以下统称 VNode )。而 render() 函数转为 VNode 的过程,是由 createElement() 函数完成的。 因此,本次文章
库层LIBRARY: (SQlite、opengl|es,surfece Manager) 和 Android Delvik Vritual Machine Application Framenwork: Activity Manager \ WiFi Manager\ Location Manager…. Applicaton : 游戏 、应用 2. ...
Bamboo Virtual ListView Control --Elmer(elmer728@126.com) 2007.06 ====================================================几个优点: 1.最明显的两个优点就是美观和高效。界面美观,定制性高;...