继承类类型的c ++容器

时间:2012-10-10 11:33:57

标签: c++ inheritance vector containers

我希望有一个容器(让我们说一个std :: vector)来容纳各种继承的类型,并实例化它们,.i.e。类向量 - >对象矢量。

例如:

class A{};

class B: public class A
{};

class C: public class A
{};

void main()
{
    std::vector<of inherited A types> typesVec;
    std::vector<A*> objectsVec;

    typesVec.push_back(class B);
    typesVec.push_back(class C);

    for (int i = 0; i < typesVec.size(); i++)
    {
        A* pA = new typesVec.at(i);
        objectsVec.push_back(pA);
    }

}

提前致谢..

4 个答案:

答案 0 :(得分:6)

这在C ++中是不可能的(至少不是直接的)。我可以看到这种情况发生在一种具有反射的语言中,但C ++却没有。

您可以做的是创建工厂或简单地创建指定类型的对象的方法。

你没有一个类型的向量,而是有一个对象生成器的向量(足够接近,对吗?):

class A{};

class B: public class A
{};

class C: public class A
{};

struct AFactory
{
    virtual A* create() { return new A; }
};
struct BFactory : AFactory
{

    virtual A* create() { return new B; }
};
struct CFactory : AFactory
{

    virtual A* create() { return new C; }
};

//...

typesVec.push_back(new BFactory);
typesVec.push_back(new CFactory);

for (int i = 0; i < typesVec.size(); i++)
{
    A* pA = typesVec.at(i)->create();
    objectsVec.push_back(pA);
}

答案 1 :(得分:0)

快速解决方案草图:

C ++标准不提供对构造函数的直接调用。因此,您不能拥有构造函数的函数指针;但是,您可以使用包装函数“create”,例如:

template<typename T>
T* create () {
   return (new T();
}

为一个参数,两个参数提供重载的创建定义,或者尝试使用可变参数模板;或者,如果您已经知道需要什么类型,则可以专门创建创建功能。然后你可以有一个指向create函数的函数指针:

&create<TheType>

请注意,此功能的签名取决于使用的类型。但是,您可以创建一个结构,其中包含模板化类型的typdef,类型指针的typedef和创建函数作为仿函数operator()

因此,您可以有两个向量,一个用于指向create函数的函数指针,或者,也可以是前面提到的结构,另一个是实际对象。在您只有继承类型的情况下,您可以为每个继承的类型B,C,...定义函数A* createB() { return new B(); }A* createC() { return new C(); },...并且有一个指针向量这些创建函数和A指针的第二个向量。

答案 2 :(得分:0)

我可能会指出Andrei Alesandrescu的书Modern C ++ Design(或他在书中描述的Loki库)和关于类型列表的章节。这将要求您在编译时执行typeVec.insert( type )

答案 3 :(得分:0)

模板有一种可重复使用的方法。这是派生类型的通用工厂,它带有installcreate方法,可以让您编写如下代码:

int main() {
    TypeVector<Base> t;
    t.install<Foo>("Foo");
    t.install<Bar>("Bar");

    t.create("Foo")->hello();
}

注意它是草图实现。在现实世界中,您可以提供另一个模板参数来指定基础容器类型(对于少数类型,vector可能比set更有效。)

类型向量是这样的:

模板     class Creator;

template <typename Base>
class TypeVector {
public:
    template <typename Derived>
    void install (std::string const &name) ;

    std::shared_ptr<Base> create (std::string const &name) const;

private:
    struct Meta {
        Meta(std::shared_ptr<Creator<Base>> creator, std::string const &name)
            : creator(creator), name(name) {}

        std::shared_ptr<Creator<Base>> creator;
        std::string name;
    };

    std::vector<Meta> creators_;
};

我们需要一种以可分配方式存储类型的方法。我们这样做boost::shared_ptr,它结合了抽象基类和模板派生类:

template <typename Base>
class Creator {
public:
    virtual ~Creator() {}        
    virtual std::shared_ptr<Base> create() const = 0;
};

template <typename Base, typename Derived>
class ConcreteCreator : public Creator<Base> {
public:
    virtual std::shared_ptr<Base> create() const {
        return std::shared_ptr<Base>{new Derived()};
    }
};

“具体创建者”能够分配一个实际的对象,并返回它的指针。

最后,以下是TypeVector::installTypeVector::create的实现:

template <typename Base>
template <typename Derived>
void
TypeVector<Base>::install (std::string const &name)
{
    creators_.emplace_back(
        std::shared_ptr<Creator<Base>>(new ConcreteCreator<Base, Derived>()),
        name);
}

template <typename Base>
std::shared_ptr<Base> 
TypeVector<Base>::create (std::string const &name) const
{
    for (auto m : creators_) {
        if (name == m.name) return m.creator->create();
    }
    throw std::runtime_error("...");
}

最后,这是一个测试:

#include <iostream>
struct Base {
    virtual ~Base() {}
    virtual void hello() const = 0;
};
struct Foo : Base {
    virtual void hello() const { std::cout << "I am a Foo\n"; }
};
struct Bar : Base {
    virtual void hello() const { std::cout << "I am a Bar\n"; }
};

int main() {
    TypeVector<Base> t;
    t.install<Foo>("Foo");
    t.install<Bar>("Bar");

    t.create("Foo")->hello();
}

你可以更进一步,使任何构造函数可以调用代码,如...

...
    Bar(Color, Age, int)
...
t.create("Foo", Color::Red, Age::TooOld, 42)

...但是这需要对可变参数模板参数列表的精辟掌握,以及如何将它们折叠成构造函数调用(可以完成并且已经完成,但它会爆炸这个答案)。

相关问题