Hi,
As part of a personal project with Unreal Engine 4, I'm learning c++. After much deliberation I think I'm finally starting to understand some new concepts which you simply don't see with managed languages. My goals here are as follows;
1. To manage all memory properly to avoid any memory leaks
2. To have all code as compact as possible while not being ugly
3. To abide by c++ standards as much as possible
If you could point out any changes you'd make to this code I've written, please let me know. Once I know how I should be writing the code properly, I can confidently apply that to the rest of my project as I write it.
This class is called ContainerAttributes, and is effectively a dictionary of names and values, for example capacity is 100, health is 75 etc. As different objects will have different storage information, eg. some items may be invincible and may have no health attributes, I'm not hardcoding anything in, rather I'm allowing the programmer to specify the attributes and values dynamically.
.h:
#pragma once
#include "DebugHelper.h"
#include "CoreMinimal.h"
class NOCTURNALSYNERGY_API ContainerAttributes
{
public:
ContainerAttributes();
~ContainerAttributes();
void SetValue(string name, int value);
int GetValue(string name);
void Debug();
private:
map<string, int> values;
};
As I mentioned, I have no experience with memory management but I understand that anything created with 'new' returns a pointer to the object which must be deleted to free memory using 'delete'. To the best of my knowledge as well, ~ methods are deconstructors, which would come about in the following situation;
obj->containerAttributes = new ContainerAttributes();
obj->whatever();
delete obj;
(then inside obj)
obj::~obj()
{
delete containerAttributes;
}
This should consequently call the deconstructor in ContainerAttributes which can free its memory (the values map).
.cpp:
#include "ContainerAttributes.h"
ContainerAttributes::ContainerAttributes()
{
values = new map<string, int> {};
}
ContainerAttributes::~ContainerAttributes()
{
delete values;
}
void ContainerAttributes::SetValue(string name, int value)
{
auto it = values.find(name);
if(it != values.end())
{
values[name] = value;
}
else
{
values.insert(pair<string, int>(name, value));
}
}
int ContainerAttributes::GetValue(string name)
{
auto it = values.find(name);
if(it != values.end())
{
return it->second;
}
else
{
DebugHelper::Debug("Error in ContainerAttributes; map does not contain name '" + name + "'");
}
}
void ContainerAttributes::Debug()
{
map<string, int>::iterator it;
for(it = values.begin(); it != values.end(); it++)
{
DebugHelper::Debug(it->first + ": " + it->second);
}
}
A few other questions regarding this, DebugHelper::Debug accepts an FString. Is it even possible to concatenate strings in this way using '+' or do I have to use another method? I'm happy to create an additional method to accept std::string as well, but I'm not sure whether '+' will work there either.
Finally, is it best to use
auto it = values.find(name);
or should I explicitly specify something like this?
pair<string, int> it = values.find(name);
Thanks for any help you can give.
What I have tried:
--------------------------------------------------------------------------