函数参数的多态性
好吧——这可能是一个非常愚蠢的问题,但它一直困扰着我。
有没有一种语言
class Animal;
class Ape : public Animal
{...}
void doStuff(Animal* animalPtr)
{
cout << "doing animal stuff" << endl;
}
void doStuff(Ape* apePtr)
{
cout << "doing ape stuff" << endl;
}
Animal *ape = new Ape();
doStuff(ape);
可以产生“doing ape stuff”
? (请耐心等待我使用 C++ 语法) 为了澄清,我想要“一个接受参数并根据参数的类型对其进行操作的函数”。
这有意义吗?当然,作为开发人员,您需要小心,因为看起来像 Animal 指针的实例实际上可能调用 Ape 代码,因为在运行时它指向的是 Ape 实例。
Ok - this may be a very stupid question, but it's been bothering me.
Is there a language where
class Animal;
class Ape : public Animal
{...}
void doStuff(Animal* animalPtr)
{
cout << "doing animal stuff" << endl;
}
void doStuff(Ape* apePtr)
{
cout << "doing ape stuff" << endl;
}
Animal *ape = new Ape();
doStuff(ape);
would yield "doing ape stuff"
? (please bear with me using C++ syntax)
To clarify, I want "a function that accepts an argument and acts on it according to the type of the argument".
And would it make sense? Of course, as a developer you'd need to take care since instances that look just like an Animal pointer might actually call Ape code, because at runtime it's an Ape instance being pointed to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,有!这称为多重调度。维基百科的文章非常好。遗憾的是,它似乎只能通过大多数流行语言的语言扩展来支持,但有一些(大多数是深奥的)语言本身就支持它。
Yes, there are! This is called multiple dispatch. The Wikipedia article is very good. Sadly, it seem to only be supported via language extensions for most popular languages, but there are a few (mostly esoteric) languages which support it natively.
查看访问者模式
Take a look at the Visitor pattern
有可能,但它不会是 C++,因为 C++ 中的函数重载查找是在编译时完成的,而不是此处所需的运行时完成的。它需要一种允许类型提示和重载的动态语言。
Potentially, but it would not be C++ since the function overloading lookups in C++ are done at compile-time, not runtime as would be required here. It would require a dynamic language that allows type-hinting and overloading.
这里有一些不一致之处,令人困惑。您是否想要一个接受参数并根据参数的类型对其进行操作的函数?这并不是真正的多态性,因为这些函数是独立的,它们不是属于类或接口层次结构的方法。换句话说,这有点像将面向对象范式与过程范式混合在一起。
如果您要参数化的是类型而不是变量,则可以使用 Java 泛型之类的东西。使用泛型,您可以通知方法传入的参数类型也是可变的。该方法以通用方式作用于变量类型。
There is some inconsistency here that is confusing. Do you want a function that accepts an argument and acts on it according to the type of the argument? This wouldn't really be polymorphism as the functions are on their lonesome, they aren't methods belonging to a class or interface hierarchy. In other words, its kindof like mixing OO paradigms with procedural paradigms.
If it is the type you want to parameterize and not the variable, you would use something like Java Generics. With generics you can inform a method that the type of the parameter coming in will also be variable. The method acts on the variable type in a generic fashion.
C++ 的开放式多方法,作者:Peter Pirkelbauer、Yuriy Solodkyy 和比亚恩·斯特鲁斯特鲁普。
本文讨论了将多种方法集成到 C++ 中的语言扩展以及一些非常有趣的实现细节,例如处理动态加载的库和正确分派的实际布局。请注意,它还不是 C++ 标准的一部分,并且可能不是任何主要编译器的一部分。
Open Multi-Methods for C++, by Peter Pirkelbauer, Yuriy Solodkyy, and Bjarne Stroustrup.
This paper discusses a language extention to integrate multi-methods into C++ along with some very interesting implementation details, such as dealing with dynamically loaded libraries and the actual layout for dispatching properly. Note that it is not yet part of the C++ standard, and probably not a part of any major compiler.