将函数指针插入到具有多态返回类型的映射中

时间:2012-11-19 10:01:53

标签: c++

我不知道如何标题这个问题。我有一个基类和两个继承类:

class Base {};
class Action : public Base {};
class Title : public Base {};

现在假设我有两个返回Action *Title *的函数:

Action *getAction() { return new Action; }
Title *getTitle() { return new Title; }

有没有办法将这两个功能放入地图?像这样:

int main()
{
    std::map<std::string, Base *(*)()> myMap;
    myMap["action"] = getAction;
    myMap["title"] = getTitle;

    return 0;
}

现在我收到一个错误:

invalid conversion from `Action* (*)()' to `Base* (*)()'

我可以将函数的签名更改为始终返回基类,然后它可以工作,但我想知道是否有另一种方法可以解决这个问题。

2 个答案:

答案 0 :(得分:2)

如果您使用:

Base *getAction() { return static_cast<Base *>(new Action); }
Base *getTitle() { return static_cast<Base *>(new Title); }

然后你不会得到这个错误。

std::function是STL提供的多态函数指针包装器。

当然,使用模板,您可以编写自己的函数包装器来存储目标,传递参数并进行转换。虽然这已经完成了,但在你决定自己动手之前,你应该认真考虑。除非你喜欢重新发明轮子或有非常特殊的要求。

答案 1 :(得分:1)

作为概念证明,我有这个代码:

#include <iostream>
#include <map>
#include <functional>

struct A
{
    virtual void f() = 0;
};

struct B : public A
{
    void f() { std::cout << "B::f\n"; }
};

struct C : public A
{
    void f() { std::cout << "C::f\n"; }
};

B* getB() { return new B; }
C* getC() { return new C; }

int main()
{
    std::map<std::string, std::function<A*()>> m;
    m["b"] = getB;
    m["c"] = getC;

    m["b"]()->f();
    m["c"]()->f();
}

它会泄漏内存,但works