问题
Why can default arguments in C++ functions be considered polymorphism?
Why is it consider ad-hoc polymorphism and universal 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?
回答1:
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.
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.
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).
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!");
and it would simply print "Hello World!", or we can call it like this:
Car mycar;
a_function("Hello World!", &mycar);
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
{
// ...
};
and call it like this:
DieselTruck mytruck;
a_function("Hello Earth!", &mytruck);
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.
回答2:
1) It can be considered polymorphism because you're effectively providing different ways of calling the same function. Consider:
int func(int a = 1, int b = 2, int c = 3, int d = 4);
I can call this the following ways:
func();
func(5);
func(5, 6);
func(5, 6, 7);
func(5, 6, 7, 8);
This way, it's similar to function overloading if we had provided these 5 different methods without any default arguments:
int func();
int func(int a);
int func(int a, int b);
int func(int a, int b, int c);
int func(int a, int b, int c, int d);
2) I, too, had not come across the term ad-hoc polymorphism until today, but my opinion is that it's ad-hoc because the number of types we can use, and the number of ways you can use it are all pre-determined.
来源:https://stackoverflow.com/questions/4638852/default-arguments-in-function-and-polymorphism