Depending on what you are doing, I would recommend that you check out the Standard Template Library (STL). It makes dynamic memory allocation and management a snap, and there are a few tricks that you can do to make accessing the varaibles much easier. At first glance the vector seems to be what you are looking for, because it is an array that will dynamically grow and manage its own memory. However there are problems with using an array, even if you are doing it the straight C++ way. As long as you will be using STL, I would recommend the map. A map you have a key, and a value that the key represents. You could make the key a string, and the value a double. Here is an example.
//There is a little bit of setup work here, but do not let this intimidate you.
//This goes in your header file.
#include using std::map;
// This is a function object that is declared to help sort the string names in your map.
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef map mapVars;
Here is how you will use what we just setup:
mapVars vars;
// I read in your question that creating a name for your variables is not an issue, so for
// simplicity I am going to use a statically defined string.
// Insert a new variable.
vars.insert("variable1", double*);
// Lookup the new variable.
double value = vars["variable1"];
//Remove this variable. when you erase a variable from a vector or a dynamic array, you will have
//gaps, this is transparently handled with a map.
vars.erase("variable1");
Now you have a set of dynamically defined variables with names. Now you need a way to dynamically allocate the array of variables. This case would either be good for a vector or a map. Both of these objects will manage their own memory, and grow automatically for your. Here is how you would use the vector:
// Once again, this goes into your header file.
// Also use the same code as above for the map.
#include
using std::vector;
typedef vector vecDouble;
typedef map, ltstr> mapVecVars;
// Here is how to use your new dynamic 2 dymensional variable vectors.
// First create the new varaible, which will actually be a vector.
mapVecVars vars;
vecDouble newVar;
// Add new doubles values into your new var.
newVar.push_back(1.0);
newVar.push_back(3.14);
newVar.push_back(98.6);
// A