Let's say I have a simple C++ class that is used to store some data:
class MyClass
{
std::string Name;
int Data = 0;
MyClass(std::string n, int d) : Name(n), Data(d)
{ }
};
Now I would like to have some kind of management class, which stores a mapping of instances to MyClass
. I want to use that second class to access these instances later at runtime.
class MyClassMgmt
{
std::map<std::string, MyClass*> Mapping;
MyClassMgmt()
{ }
void AddMyClass(MyClass* MyCls)
{
Mapping[MyCls->Name] = MyCls;
}
MyClass* GetMyClass(const std::string& Name) const
{
return Mapping.at(Name);
}
};
My question is: If I would instead use MyClass
directly with the help of static members to implement this management functionality, does that have a negative impact on overall performance then? Or are there any other drawbacks with this approach?
Something like this:
class MyClass
{
std::string Name;
int Data = 0;
static std::map<std::string, MyClass*> Mapping;
MyClass(std::string n, int d) : Name(n), Data(d)
{ }
static void AddMyClass(MyClass* MyCls)
{
Mapping[MyCls->Name] = MyCls;
}
static MyClass* GetMyClass(const std::string& Name) const
{
return Mapping.at(Name);
}
};