加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在c中创建一个类数组?

发布时间:2020-12-16 03:32:11 所属栏目:百科 来源:网络整理
导读:码: struct Base { ... };struct A : public Base { ... };struct B : public Base { ... };struct C : public Base { ... }; 是否可以创建一个包含这种类型结构的数组? 样本/预期结果: Type inheritedTypesOfStruct[3] = {A,B,C}; 这样做的目的是我后来
码:
struct Base { ... };

struct A : public Base { ... };
struct B : public Base { ... };
struct C : public Base { ... };

是否可以创建一个包含这种类型结构的数组?
样本/预期结果:

Type inheritedTypesOfStruct[3] = {A,B,C};

这样做的目的是我后来想要创建一个具有从数组中检索的随机类的对象.

解决方法

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <vector>
#include <memory>

using namespace std;




// interface
class Base
{
public:
    virtual ~Base() { }
    virtual int getClassId() = 0;
};


// class A relizes interface Base,has ID == 1 (is used in automatic registration to factory)
class A : public Base
{
public:
    const static int ID = 1;
    static Base* CreateInstance()
    {
        return new A();
    }

    virtual int getClassId()
    {
        return ID;
    }

    virtual ~A() { }
};


// class B relizes interface Base,has ID == 2 (is used in automatic registration to factory)
class B : public Base
{
public:
    const static int ID = 2;
    static Base* CreateInstance()
    {
        return new B();
    }

    virtual int getClassId()
    {
        return ID;
    }

    virtual ~B() { }
};



// this is the objects factory,with registration only (unregister s not allowed)
class ObjectFactory
{
    ObjectFactory() { }
    ObjectFactory(ObjectFactory&) { }
public:
    virtual ~ObjectFactory() { }
    static ObjectFactory& instance()
    {
        static ObjectFactory objectFactory;

        return objectFactory;
    }

    typedef Base* (*Creator) ();

    void registerCreator(int id,Creator creator)
    {
        registry[id] = creator;
    }

    Base* CreateById(int id)
    {
        return registry[id]();
    }

private:
    map<int,Creator> registry;
};


// this template class is used for automatic registration of object's creators
template <class T>
struct RegisterToFactory
{
    RegisterToFactory(ObjectFactory& factory)
    {
        factory.registerCreator(T::ID,&T::CreateInstance);
    }
};


namespace
{
    // automaticaly register creators for each class
    RegisterToFactory<A> autoregisterACreator(ObjectFactory::instance());
    RegisterToFactory<B> autoregisterBCreator(ObjectFactory::instance());
}




// lets this this solution
int main(int argc,char *argv[])
{
    vector<int> ids;

    ids.push_back(static_cast<int>(A::ID));
    ids.push_back(static_cast<int>(B::ID));

    srand(time(0));

    for (int i = 0; i < 20; ++i)
    {
        int randomClasssId = ids[rand() % ids.size()];
        auto_ptr<Base> testObject(ObjectFactory::instance().CreateById(randomClasssId));
        cout << "Object of classId = " << testObject->getClassId() << " has been produced by factory." << endl;
    }


    system("PAUSE");
    return EXIT_SUCCESS;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读