I'm doing a project that implements a Python style console. It has several functions, it has many things. But I need to implement a command called set
. set
would declare a string and set its value as the user defined it.
class Variavel
{
private:
char* nome;
char* valor;
public:
Variavel() : nome(NULL), valor(NULL)
{
}
~Variavel()
{
delete[] nome;
delete[] valor;
}
void DefVar(const char* valor, const char* nome)
{
this->valor = new char[strlen(valor)];
this->valor = const_cast<char*>(valor);
this->nome = new char[strlen(nome)];
this->nome = const_cast<char*>(nome);
}
};
I could create a large array or a pointer, and with each instance that the client defined another variable, the array number would increase. When it asks for a variable, it will map each array to the end. It is a method that works, but it is very slow, it spends a lot of memory and it is a bit inefficient. How to create a runtime variable in an efficient way?