虚拟函数可以有默认参数吗? [英] Can virtual functions have default parameters?

查看:104
本文介绍了虚拟函数可以有默认参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个基类(或接口类)并为其一个或多个参数指定一个默认值,那么派生类必须指定相同的默认值,如果没有,那么默认值将显示在派生类中?

If I declare a base class (or interface class) and specify a default value for one or more of its parameters, do the derived classes have to specify the same defaults and if not, which defaults will manifest in the derived classes?

附录:我也想了解如何在不同的编译器中处理这些问题,以及在这种情况下对推荐做法的任何输入。

Addendum: I'm also interested in how this may be handled across different compilers and any input on "recommended" practice in this scenario.

推荐答案

虚拟可能有默认值。基类中的默认值不会被派生类继承。

Virtuals may have defaults. The defaults in the base class are not inherited by derived classes.

使用哪个默认值 - 即基类或派生类' - 由用于调用函数的静态类型确定。如果通过基类对象,指针或引用来调用,则使用基类中表示的默认值。相反,如果你通过一个派生类对象,指针或引用使用派生类中表示的默认值。在标准报价下面有一个例子演示了这一点。

Which default is used -- ie, the base class' or a derived class' -- is determined by the static type used to make the call to the function. If you call through a base class object, pointer or reference, the default denoted in the base class is used. Conversely, if you call through a derived class object, pointer or reference the defaults denoted in the derived class are used. There is an example below the Standard quotation that demonstrates this.

一些编译器可能做不同的事,但这是C ++ 03和C ++ 11标准:

Some compilers may do something different, but this is what the C++03 and C++11 Standards say:

EDIT :C ++ 11标准说的完全一样)

(EDIT: The C++11 Standard says exactly the same thing)


虚函数调用(10.3)使用
虚函数

声明中的参数通过表示对象的指针或引用的静态类型确定
。派生
类中的
重写函数不会从
覆盖的函数中获取默认参数。 [示例:

A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides. [Example:



struct A {
  virtual void f(int a = 7);
};
struct B : public A {
  void f(int a);
};
void m()
{
  B* pb = new B;
  A* pa = pb;
  pa->f(); //OK, calls pa->B::f(7)
  pb->f(); //error: wrong number of arguments for B::f()
}




—end example]



b $ b

编辑这里是一个示例程序,用于演示默认值。我在这里使用 struct s而不是 class es简单 - class struct 几乎在所有方面都完全相同,除了默认可见性。

Edit Here is a sample program to demonstrate what defaults are picked up. I'm using structs here rather than classes simply for brevity -- class and struct are exactly the same in almost every way except default visibility.

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

using std::stringstream;
using std::string;
using std::cout;
using std::endl;

struct Base { virtual string Speak(int n = 42); };
struct Der : public Base { string Speak(int n = 84); };

string Base::Speak(int n) 
{ 
    stringstream ss;
    ss << "Base " << n;
    return ss.str();
}

string Der::Speak(int n)
{
    stringstream ss;
    ss << "Der " << n;
    return ss.str();
}

int main()
{
    Base b1;
    Der d1;

    Base *pb1 = &b1, *pb2 = &d1;
    Der *pd1 = &d1;
    cout << pb1->Speak() << "\n"    // Base 42
        << pb2->Speak() << "\n"     // Der 42
        << pd1->Speak() << "\n"     // Der 84
        << endl;
}

此程序的输出(在MSVC10和GCC 4.4上) p>

The output of this program (on MSVC10 and GCC 4.4) is:

Base 42
Der 42
Der 84

这篇关于虚拟函数可以有默认参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆