How can I map class types to instances of that class in C++?

半腔热情 提交于 2020-01-23 18:48:50

问题


I wish to have a map from classes to instances of each class. The goal is to have a container of components for a component-based game engine where an object can have at most one of each component type.

In Java, I could just use class objects as the key. How can I do something similar in C++? My initial research suggests something like using typeid(component_instance).name() as the key. Is there a better way to do this?


回答1:


Unlike in more dynamic languages like Python or Java, classes in C++ are not objects themselves. At runtime they merely do not exist (as for a programmers point of view).

Your typeid approach is not that bad, but for performance issues I'd use a hash or a numeric ID (like an integer defined as static in your class) instead of the string.




回答2:


C++ classes are not reified so you don't have class objects.

You might consider having your own class descriptors (as a convention). You could even make your own preprocessor for that.

Perhaps you could study and take inspiration from Qt meta-object system, which uses a preprocessor called moc. Probably, using exactly Qt objects is simpler than inventing your own meta-class system.




回答3:


You could create unique keys for types like this (note that this is for single-threaded execution only):

struct TypeManager
{
  private:
    static int next_id(void)
    {
        static int id = 0;
        return ++id;
    }
  public:
    template<typename T>
    static int get_type_id(void)
    {
        static int const id = next_id();
        return id;
    }
};

Usage:

int unique_bool_id = TypeManager::get_type_id<bool>();
int unique_float_id = TypeManager::get_type_id<float>();



回答4:


In JAVA, a Singleton class ensures a class has only one instance.

Also in C++, if you want to create only one instance of a class then the Singleton patterns are used. The Singleton Design pattern is used, where only one instance of an object is needed throughout the lifetime of an application.

The Singleton class is instantiated at the time of first access and same instance is used thereafter till the application quits.

The Singleton Design pattern is often used to control access to resources such as database connections or sockets. Suppose we have a license for only one connection for our database. A Singleton connection object makes sure that only one connection can be made at any time.

#include <iostream>
using namespace std;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {
        //private constructor

    }
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {
        instanceFlag = false;
    }
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;

Singleton* Singleton::getInstance()
{
    if(! instanceFlag)  // use the instanceFlag in all methods and functions to check if object already exists 
    {
        single = new Singleton();
        instanceFlag = true;
        cout<<"NO OBJECT WAS PREVIOUSLY CREATED"<< endl;
        cout<<"__________________________________________________"<< endl;

        return single;
    }
    else
    {
        cout<<"OBJECT ALREADY EXISTS!!"<< endl;
        cout<<"__________________________________________________"<< endl;
        return single;
    }
}

void Singleton::method()
{
     cout << "Method of the singleton class" << endl;
}

int main()
{
    Singleton *sc1,*sc2;
    cout<<"Object 1"<< endl;
    sc1 = Singleton::getInstance(); 
    cout<<"Object 2"<< endl;
    sc2 = Singleton::getInstance(); 


    return 0;
}


来源:https://stackoverflow.com/questions/8562994/how-can-i-map-class-types-to-instances-of-that-class-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!