简体   繁体   中英

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

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?

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.

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.

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>();

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM