热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

虚函数C++中的默认参数

如何解决《虚函数C++中的默认参数》经验,为你挑选了1个好方法。

我读到了C++中的继承机制和虚函数.

根据我的知识(在我遇到的所有例子中),继承的方法与父类具有相同的签名.

我的问题如下:我知道函数默认参数值不是函数签名的一部分.

我可以将此值定义为父类虚函数中的某个常量,并且在派生类中声明并实现没有此默认值的重写方法.

在这种情况下,当我使用指向父类的指针调用派生对象的方法时,是否会使用/不使用此默认初始化来调用函数?

谢谢



1> 5gon12eder..:

默认参数主要是语法糖,并在编译时确定.另一方面,虚拟调度是一种运行时功能.如果选择的默认参数与实际调用的函数一起定义可能最不令人惊讶,但由于上述原因,这是不可能的(至少没有额外的运行时开销).

因此,编译器使用调用成员函数的对象的静态类型来选择默认参数.我们来看一个例子吧.

#include 
#include 

class Base
{

public:

  virtual void
  f(int a, int b = 1)
  {
    std::cout <<"Base: a = " < base_as_base {new Base {}};
  std::unique_ptr derived_as_base {new Derived {}};
  std::unique_ptr derived_as_derived {new Derived {}};
  base_as_base->f(0);        // Base:    a = 0, b = 1
  derived_as_base->f(0);     // Derived: a = 0, b = 1
  // derived_as_base->f();   // compiler error
  derived_as_derived->f(0);  // Derived: a = 0, b = 2
  derived_as_derived->f();   // Derived: a = 1, b = 2
}

我同意这令人困惑.请不要写这样的代码.幸运的是,有一个简单的解决方法.除了根本不使用默认参数之外,我们可以使用称为非虚拟接口的习语.制作虚拟功能protected,但不提供任何默认参数.然后,它仅由virtual基类中的非函数间接调用.该函数可以在一个地方定义所有默认参数.

#include 
#include 

class Base
{

public:

  void
  f(int a, int b = 1)
  {
    this->impl(a, b);
  }

protected:

  virtual void
  impl(int a, int b)
  {
    std::cout <<"Base: a = " < base_as_base {new Base {}};
  std::unique_ptr derived_as_base {new Derived {}};
  std::unique_ptr derived_as_derived {new Derived {}};
  base_as_base->f(0);        // Base:    a = 0, b = 1
  derived_as_base->f(0);     // Derived: a = 0, b = 1
  derived_as_derived->f(0);  // Derived: a = 0, b = 1
}


推荐阅读
author-avatar
php辉子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有