作者:php辉子 | 来源:互联网 | 2023-05-26 15:37
我读到了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
}