`

c++ - vritual function and default arguments

    博客分类:
  • c++
c++ 
阅读更多

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;
}
 

 

 

分享到:
评论

相关推荐

    rootfs.ubuntu-base-16.04.6-base-amd64.linux-image-4.4.0-203-generic.vritual.tgz

    基于ubuntu 16.04.6的 jeos,最小的完整ubuntu系统。 直接解压tar包到ext3、ext4分区,添加grub4dos或grub2引导即可。 注意:可能需要修改解压后的/etc/fstab!

    memory vritual to physical.zip

    内存虚拟化到物理内存的转换是现代计算机系统中一个至关重要的技术,特别是在多任务和多用户的操作系统中。这个主题涉及到如何高效地管理有限的物理内存资源,为多个虚拟地址空间提供服务。以下是对这一概念的详细...

    VirtualBox-5.1.26-117224-Win.exe

    VirtualBox号称是最强的免费虚拟机软件,它不仅具有丰富的特色,而且性能也很优异!它简单易用,可虚拟的系统包括Windows(从Windows 3.1到Windows10、Windows Server 2012,所有的Windows系统都支持)、Mac OS X、...

    VirtualBox-5.2.44-139111-Win.zip

    《VirtualBox 5.2.44-139111-Win:虚拟化技术的卓越工具》 VirtualBox,作为一个开源且跨平台的虚拟化软件,是全球许多企业和个人用户的重要工具,用于在一台物理计算机上运行多个操作系统。这款软件由德国公司...

    visual-studio-code:DarkVisual Studio Code的深色主题

    《深色主题:Visual Studio Code中的Dracula主题详解》 Visual Studio Code,简称VS Code,是微软开发的一款轻量级且强大的源代码编辑器,它支持多种编程语言,并且具有丰富的扩展功能。对于长时间编程的开发者来说...

    Centos7下安装vsftpd并启用虚拟用户访问

    chown virtual:vritual /var/ftp/share -R ``` 4. **设置目录权限**: ```bash chmod -R 755 /var/ftp/share ``` #### 三、虚拟用户账号与数据库配置 接下来需要创建虚拟用户的登录文件和数据库文件。 1. *...

    HipChat Server 破解

    1、请到官网下载hipchat server 虚拟机模板 https://hipchat-server-stable.s3.amazonaws.com/HipChat.ova 2、Vritual box 导入虚拟机 3、参照 hipChat破解手册.doc 的破解步骤进行破解

    SQLiteVTable:SQLite虚拟表示例

    您可以使用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软件参数设置中Display面板参数的含义-经验文摘

    Allegro是一款广泛应用于电子设计自动化(EDA)领域的PCB设计软件,它能够帮助工程师完成电路板的设计、分析和制造等工作。在使用Allegro进行PCB设计时,软件提供了丰富的参数设置选项,其中Display面板参数对于控制...

    Qt5虚拟键盘支持拖动中文拼音,大小修改

    在本文中,我们将深入探讨如何在Qt5框架中实现虚拟键盘功能,并且特别关注如何让虚拟键盘支持中文拼音输入、大小调整以及鼠标拖动。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备。...

    pyvda:用于操作Windows 10虚拟桌面的VirtualDesktopAccessor的Python实现

    Python虚拟桌面访问器Python模块提供对通过访问的大多数设置的编程访问。 包括切换虚拟桌面,在虚拟桌面之间移动窗口,固定窗口以及列出桌面上的窗口。 最初基于 。安装pip install pyvda用法示例from pyvda import ...

    RP4VM详细安装部署教程.docx

    在验证 RP4VM 且测试保护虚拟机时,需要登录 vCenter,主页,可以查看到生成的插件 RecoverPoint for VMs,选择要保护的虚拟机,右键,所有 RecoverPoint for vritual Machines 操作为这个虚拟机创建一个新的一致性...

    Vue 中 template 有且只能一个 root的原因解析(源码分析)

    引言 今年, 疫情 并没有影响到各种面经的正常出现,可谓是...然后,再由 render() 函数转为 Vritual DOM (以下统称 VNode )。而 render() 函数转为 VNode 的过程,是由 createElement() 函数完成的。 因此,本次文章

    Android下的Linux指令

     库层LIBRARY: (SQlite、opengl|es,surfece Manager) 和 Android Delvik Vritual Machine  Application Framenwork: Activity Manager \ WiFi Manager\ Location Manager….  Applicaton : 游戏 、应用  2. ...

    [原创]Barmboo Virtual ListView(虚拟ListView控件)演示DEMO

    Bamboo Virtual ListView Control --Elmer(elmer728@126.com) 2007.06 ====================================================几个优点: 1.最明显的两个优点就是美观和高效。界面美观,定制性高;...

Global site tag (gtag.js) - Google Analytics