函数和多态性中的默认参数 [英] Default arguments in function and polymorphism

查看:247
本文介绍了函数和多态性中的默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 为什么C ++函数中的默认参数被视为多态性?

  1. Why can default arguments in C++ functions be considered polymorphism?

和通用多态性?

我认为它可以被认为是一个自组织多态性, ?),但是它是什么方式是通用多态性?

I think that it can be considered as an ad-hoc polymorphism as its like overloading functions(?), but in what way is it universal polymorphism?

推荐答案

如果某物是多态的,它有许多形状。

If something is polymorphic, it has many shapes.


在计算机科学中,多态是一种
编程语言特性,
允许不同数据类型的值
使用统一的
接口处理。

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.

由于 this 链接解释,有不同的类型的多态性。当接口根据单独指定的类型和组合的有限范围定义不同的实现时,自组织多态性是我们所谓的。函数重载和默认参数是一个很好的例子。

As this link explains, there are different types of polymorphism. Ad-hoc polymorphism is what we call it when an interface defines different implementations depending on a limited range of individually specified types and combinations. Function overloading and default parameters is a great example of this.

通用多态性被分解为参数多态性(当一个接口是类型无关的 - 模板是一个很好的例子)和包含多态性(当一种数据类型可以作为另一种处理时,即典型的OOP多态性,其中派生类可以被视为基类)。

Universal polymorphism is split into parametric polymorphism (when an interface is type independent - templates is a good example of this) and inclusion polymorphism (when one data type can be treated as another, i.e. the typical OOP polymorphism where a derived class may be treated as a base class).

当涉及C ++中的默认函数参数时,自组织多态性是非常明显的。通用多态性可能来自于您可以使用函数参数使用C ++的包含多态特征:

When it comes to the default function arguments in C++, the ad-hoc polymorphism is quite obvious. The universal polymorphism might come from the fact that you can use C++'s inclusion polymorphic features with the function arguments:

void a_function (const char* print_me, Car* pCar = NULL)
{
    std::cout << print_me << std::endl;
    if(pCar)
        pCar->Drive();
} 

现在,我们可以像这样调用函数:

Now, we could call the function like this:

a_function("Hello World!");

,它只是打印Hello World!,或者我们可以这样调用:

and it would simply print "Hello World!", or we can call it like this:

Car mycar;
a_function("Hello World!", &mycar);

,它也会开车。这是自组织多态性。由于C ++的包含多态性你也可以这样做:

and it would also drive the car. That is ad-hoc polymorphism. Due to C++'s inclusion polymorphism you can also do this:

class DieselTruck : public Car
{
     // ...
};

并像这样调用:

DieselTruck mytruck;
a_function("Hello Earth!", &mytruck);

它会驱动卡车(如果类正确设置,所有)。所以你可以混合ad-hoc和通用多态性在函数的方式。无论这是你的教授正在考虑与否,我不知道。它不直接连接到默认参数 - 即,没有它们,你可以做到这一点完美,但是我不能看到如何使用 默认参数在C ++中实现通用多态性。

and it would drive the truck (if the classes were correctly set up, of course - virtual functions and all that). So you can mix ad-hoc and universal polymorphism in functions that way. Whether this is what your professor is thinking about or not, I do not know. It is not directly connected to default arguments - i.e. you can do this perfectly fine without them, but then again, I can't see how you could accomplish universal polymorphism in C++ using only default arguments.

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

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