繁体   English   中英

C ++方法重载:基本和派生参数

[英]C++ method overloading: base and derived parameters

在网上搜索后,我找不到这个问题的答案:

我有这个重载的方法:

foo(Base* base);
foo(Derived* derived);

在这种情况下,“Derived”是“Base”的子类。
我打电话的时候:

foo(new Derived());

我注意到总是调用第一个重载方法,而我想实现相反的结果(调用以“Derived *”对象作为参数的方法)。

怎么解决这个? 谢谢。

编辑:

好的,这是我的实际情况:

我有一个UIWidget和一个UIScoreLabel类。 UIScoreLabel派生自UIWidget。 我还有一个GameEvent类(B​​ase)和一个P1ScoreGameEvent类(Derived)。

UIWidget:

virtual void handleGameEvent(GameEvent* e) { printf("ui_widget"); }

UIScoreLabel:

virtual void handleGameEvent(P1ScoreGameEvent* e) { printf("ui_score_label"); }

这是电话:

UIWidget* scoreLabel = new UIScoreLabel();
scoreLabel.handleGameEvent(new P1ScoreGameEvent());

输出:

ui_widget

我不明白我做错了什么。

实际上,我得到了相反的结果,使用了更多Derived类型优先的方法。 在下面的演示代码中,默认情况下调用“Derived”的方法。 但是,您始终可以使用指针强制转换它。

#include <stdio.h>
#include <iostream>

class Foo {
    public:
    virtual void perform() {
       printf("Foo is on stage!\n"); 
    }
   virtual void dance() {
       printf("Foo is on dancing!\n"); 
   }
};

class Bar : public Foo {
   public:
   void perform() {
       printf("Bar is on stage!\n"); 
   }
   void dance() {
       printf("Bar is on dancing!\n"); 
   }
};

int m1 (Foo* foo) {
    foo->perform();
}
int m1 (Bar* foo) {
    foo->dance();
}
int main(){
    m1(new Bar); // Calls m1(Foo*)
    m1((Foo*) new Bar); // Calls m1(Bar*)
}

输出:

Bar is on dancing!
Bar is on stage!

请注意, bar的方法被称为两次(这是正确的多态行为!),但它是每个重载调用的一种不同bar方法,以消除歧义。

我设法通过更改此行来解决问题:

UIWidget* scoreLabel = new UIScoreLabel();

UIScoreLabel* scoreLabel = new UIScoreLabel();

但是,即使这解决了这个问题,我也想避免使用这个“技巧”,因为我的代码保留了一个UIWidget *对象列表,并对它们调用了handleGameEvent()方法。 如果有人知道任何其他解决方案,请分享。

编辑:

最小的可编译示例:

#include <stdio.h>
#include <iostream>
#include <vector>

class GameEvent {};
class P1ScoreGameEvent : public GameEvent {};

class UIWidget { public: virtual void handleGameEvent(GameEvent* e) { printf("ui_widget"); } };
class UIScoreLabel : public UIWidget { public: virtual void handleGameEvent(P1ScoreGameEvent* e) { printf("ui_score_label"); } };

void main()
{
    UIWidget* w1 = new UIScoreLabel();
    w1->handleGameEvent(new P1ScoreGameEvent()); // output: "ui_widget"

    UIScoreLabel* w2 = new UIScoreLabel();
    w2->handleGameEvent(new P1ScoreGameEvent()); // output: "ui_score_label"
}

注意:这实际上解决了问题,但解决方案并不优雅,因为我希望有这样的东西:

void main()
{
    vector<UIWidget*> widgets;
    widgets.push_back(new UIScoreLabel());
    widgets.push_back(new UIScoreLabel());
    // push more..

    for (unsigned int = 0; i < widgets.size(); i++)
        widgets[i]->handleGameEvent(new P1ScoreGameEvent()); // output: "ui_widget", but I want "ui_score_label"
}

在“最小可编译示例”中,handleGameEvent未声明为虚拟,因此不会应用重载。

这是因为C ++不支持双重调度。 如果将变量声明为Base,则会对其进行处理。 一旦将其类型更改为Derived,编译器就能够获得其真实类型,然后调用正确的方法。

要解决此问题,您可能需要使用访客模式

这个答案中有一个很好的讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM