And*_*ers 3 c++ inheritance boost interface
我有当前的设置:
class Interface1
{
  public:
    virtual ~Interface1() {}
    virtual void DoSomething() = 0;
};
class Interface2 : public virtual Interface1
{
  public:
    virtual ~Interface2() {}
    virtual void DoSomething() override = 0;
    virtual void DoSomethingElse() = 0;
};
class MyClass1 : public Interface1
{
  public:
    MyClass1();
    void DoSomething() override;
};
class MyClass2 : public Interface2
{
  public:
    MyClass2();
    void DoSomething() override;
    void DoSomethingElse() override;
};
int main()
{
    std::map<std::string, boost::any> items;
    items.insert(make_pair("item1", shared_ptr<Interface1>(new MyClass1())));
    items.insert(make_pair("item2", shared_ptr<Interface2>(new MyClass2())));
    auto object = items.at("item2");
    auto item = boost::any_cast<shared_ptr<Interface1>>(object);
    item->DoSomething();
    return 0;
}
当我运行此代码时,没有任何反应.MyClass2似乎没有打电话DoSomething(),这就是我想要的.如何拨打Interface1::DoSomething()实际电话Interface2::DoSomething()?我认为这是可能的,因为它们都是从彼此继承的,但我似乎无法使其发挥作用.
我想要这个的原因是因为我有一些函数只能用于继承的类Interface2,但是有些函数需要支持派生自Interface1和的类Interface2.一旦boost :: any接管我就松开了它最初的类型,但是如果我可以使用上面描述的设置它应该不是问题,所以即使我的原始类是派生的Interface2,它也可以调用相同的函数Interface1和得到相同的结果.
有没有办法按照当前设置做我想做的事情?
编辑:对不起,void在我的坏的构造函数面前,但这不是问题.
你为什么需要boost::any?
如果您需要确定之间的区别Interface1和Interface2,和你有一个std::shared_pointer存储在您的地图,然后就存储std::shared_pointer<Interface1>和使用std::dynamic_pointer_cast<Interface2>,以确定是否有一个Interface1或Interface2
例:
#include <map>
#include <memory>
#include <iostream>
class Interface1
{
  public:
    virtual ~Interface1() = default;
    virtual void DoSomething() = 0;
};
class Interface2 : public Interface1
{
  public:
    virtual ~Interface2() = default;
    virtual void DoSomethingElse() = 0;
};
class MyClass1 : public Interface1
{
  public:
    MyClass1() {}
    void DoSomething()     override { std::cout << "\t\t" << __PRETTY_FUNCTION__ << '\n'; }
};
class MyClass2 : public Interface2
{
  public:
    MyClass2() {}
    void DoSomething()     override { std::cout << "\t\t" << __PRETTY_FUNCTION__ << '\n'; }
    void DoSomethingElse() override { std::cout << "\t\t" << __PRETTY_FUNCTION__ << '\n'; }
};
int main()
{
    std::map<std::string, std::shared_ptr<Interface1>> items;
    items.emplace("item1", std::make_shared<MyClass1>());
    items.emplace("item2", std::make_shared<MyClass2>());
    auto check = [&items](const std::string& name)
        {
            auto object = items.at(name);
            auto item = std::dynamic_pointer_cast<Interface2>(object);
            if (item)
            {
                std::cout << name << " is an Interface2\n";
                item->DoSomething();
                item->DoSomethingElse();
            }
            else
            {
                std::cout << name << " is an Interface1\n";
                object->DoSomething();
            }
        };
    check("item1");
    check("item2");
    return 0;
}
输出:
item1 is an Interface1
        virtual void MyClass1::DoSomething()
item2 is an Interface2
        virtual void MyClass2::DoSomething()
        virtual void MyClass2::DoSomethingElse()
最后的一些说明:
我还质疑Interface2和之间需要虚拟继承Interface1
我不相信你需要重写DoSomething的Interface2-它已经存在通过公开继承Interface1
virtual void DoSomething() override = 0; 没必要