本文介绍了c ++函数重载的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个问题与函数重载。我将给你一个简单的例子:i have a problem with function overloading. I will show you with some simple example:class A {};class B : public A{};void somefunction(A&, A&);void somefunction(B&, B&);void someotherfunction() {...A& a1 = ...A& a2 = ......} a1和a2都是B的实例,但Both a1 and a2 are instances of B butsomefunction(a1,a2);调用void somefunction(A&, A&);我做错了什么? 编辑:确定现在我知道它不工作(感谢您的答案)。edit: Ok now i know it does not work (thanks for your answers).任何解决方案如何做到这一点?没有投射。Any solution how to do this? Without casting. edit2:确定离开了,使用类型投射,因为我想要的是不可能的。非常感谢您的帮助。 edit2: Ok left it as it is, with type casting, since something i would like to have is not possible. Thanks all for your help.推荐答案 pick:void somefunction((B&)a1, (B&)a2);您遇到此问题的原因是程序设计,而不是语言。编译器根据传入的类型选择使用哪个函数。C#将以完全相同的方式运行(确保Java也会这样)。The reason why you are having this problem is with the program design, not the language. Compiler picks which which function is used based on the types that are passed in. C# will behave in exactly the same way (pretty sure Java will too).我,你在错误的地方实现多态性。 somefunction 真的属于类 a ,应该是虚拟的。然后每当在运行时调用 a 的实例时,将调用正确类中的覆盖。It seems to me that you are implementing polymorphism in the wrong place. somefunction really belongs inside class a and should be virtual. Then whenever it's called on the instance of a at runtime the override in the right class will be called. ,真的应该是这样的:So, really it should be something like this:class a {public: virtual somefunction(a& a2) { //do stuff }}class b : public a { virtual somefunction(a& a2) { b& b2 = (b&)a2; //do stuff }}class c : public b { virtual somefunction(a& a2) { c& c2 = (c&)a2; //do stuff }}使用虚拟函数内部的最小转换,并假设同一类型的两个实例。这意味着 b.somefunction(a())将有未定义的行为。The above solution uses minimal casting inside the virtual function and assumes that the two instance of the same type. This means that b.somefunction(a()) will have undefined behaviour. on C ++ RTTI 并使用dynamic_cast,如果无法进行向下转换,则会返回NULL。 A better solution is to rely on C++ RTTI and use dynamic_cast, which will return NULL if the downcast is not possible. 此问题称为双调度问题并且在维基百科文章中描述的很像你所描述的。此外,维基百科为多次派遣提供的唯一解决方案是使用 dynamic_cast This problem is known as double dispatch problem and is described in the wikipedia article pretty much as you described it. Furthermore, the only solution that wikipedia gives for multiple dispatch is to use dynamic_cast. EDIT 确定,这已经让我烦恼了,这里是解决方案,类和两个子类。它aint漂亮,并使用一些C ++欺骗,如朋友类(实际上更好的封装,而不是相反)和转发声明。 EDIT OK, this has been bugging me, here is the solution for full double dispatch between a base class and two subclasses. It aint pretty and uses a bit of C++ trickery like friend classes (for better encapsulation actually, rather than the reverse) and forward declarations. class b;class c;class a {protected: virtual void somefunction(a& a2); //do stuff here virtual void somefunction(b& b2); //delegate to b virtual void somefunction(c& c2); //delegate to cpublic: virtual void doFunc(a& a2) { a2.somefunction(*this); } friend class b; friend class c;};class b : public a {protected: virtual void somefunction(a& a2); //do stuff here virtual void somefunction(b& b2); //do stuff here virtual void somefunction(c& c2); //delegate to cpublic: virtual void doFunc(a& a2) { a2.somefunction(*this); } friend class a;};class c : public b {protected: virtual void somefunction(a& a2); //do stuff here virtual void somefunction(b& b2); //do stuff here virtual void somefunction(c& c2); //delegate to cpublic: virtual void doFunc(a& a2) { a2.somefunction(*this); } friend class a; friend class b;};//class avoid a::somefunction(a& a2) { printf("Doing a<->a");}void a::somefunction(b& b2) { b2.somefunction(*this);}void a::somefunction(c& c2) { c2.somefunction(*this);}//class bvoid b::somefunction(a& a2) { printf("Doing b<->a");}void b::somefunction(b& b2) { printf("Doing b<->b");}void b::somefunction(c& c2) { c2.somefunction(*this);}//class cvoid c::somefunction(a& a2) { printf("Doing c<->a");}void c::somefunction(b& b2) { printf("Doing c<->b");}void c::somefunction(c& c2) { printf("Doing c<->c");} 这篇关于c ++函数重载的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 21:24