Dynamically allocated names?

Hi, I'm using the new keyword to allocate variables but I want the name to be dynamically allocated as well. Is there any way to code that or will the compiler just complain that none of the names I use later are declared?

edit: It might not be dynamic allocation, but I want the program to name the variables for me in a specific manner so i don't have to write all the permutations of variables out.
Last edited on
The name cannot be created dynamically because there would be no way for the compiler to recognize it in the code until after it's created (and if it's created at runtime... then the compiler would be unable to compile it because it hasn't been created yet).

Long story short. You can't do it.

BUT you can do something similar by creating a map where the string (name) is the key and the value is the variable:

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <map>

std::map< std::string, int >  myVars;


myVars["var1"] = 5;  // creates an initializes a variable named 'var1'
myVars["smoobie"] = 10;  // ditto

std::cout << myVars["var1"];  // prints 5 



Another perk to this is that you don't have to use new/delete. std::map takes care of the allocation/cleanup for you.
Last edited on
... so i don't have to write all the permutations of variables out

Out of interest, what are all these permutations?

Andy
Andy - 'AND' combinations of size, shape, and color. Possible sizes are large and small. Possible shapes are circle, diamond, square, and triangle. Possible colors are blue, green, red, and yellow. The combination has to be from different categories so: large 'AND' blue is valid, while circle 'AND' diamond is not. It comes up to 96 or so different combinations. Its not a lot, but I'm interested in how it would be done in case I end up with an additional attribute tacked on.

edit: Also includes not attributes so nBlue, nRed, nGreen, nYellow and likewise for shapes.

Disch - Thanks for that! I'm not very fluent with the stl, but I'll give it a shot. I'll post up once I finish the class structure. I'm looking for something that goes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string colorArray[4] = {blue, green, red, yellow, nBlue, nGreen, nRed, nYellow};
string shapeArray[4] = {circle, diamond, square, triangle, nCircle, nDiamond, nSquare, nTriangle};
string sizeArray[2] = {small, large}

for (int i = 0 ; i < sizeArray.length(); i++)
{
     for (int j = 0; j < shapeArray.length(); j++)
         {
              hypothesis* /* I want it to name based on sizeArray[i] and shapeArray[j]*/
          }
     for (int k = 0; k < colorArray.length(); j++)
         {
              hypothesis* /* named by size array and color array*/
          }
}

for (int q = 0; q < colorArray.length(); q++)
{
     for(int p = 0; p < shapeArray.length; p++)
          {
               hypothesis* /* named for color and shape*/
           }
}


actually, thinking about it now. Its not very important that they have names. I think I'm just gonna shove them in an array and be done with it.
Last edited on
Topic archived. No new replies allowed.