use variable in variable name

Oct 29, 2011 at 3:41pm
Is it possible to have a variable n, and spawn a new object as onbjectn? For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct block {
int x, y, z, size, rotation, velocity;
};
int nofblocks = 0;
//...
//...
int main () {
new block namegoeshere;
nofblocks = nofblocks++;
new block othernamehere;
nofblocks = nofblocks++;
//...
//...
return 0;}


Of course, namegoeshere and othernamehere would be replaced with the required syntax. It would create objects block0 and block1, theoretically repeatable infintely (block2, 3, etc.). Is it possible?

Thanks!
Last edited on Oct 29, 2011 at 3:42pm
Oct 29, 2011 at 3:44pm
What you're looking for are arrays/vectors.

1
2
3
4
5
6
7
8
9
#include <vector>
[...]
vector<block> blocks;
blocks.push_back(block(...));
blocks.push_back(block(...));

blocks[0]; //first block
blocks[1]; //second block
blocks.size(); //current number of blocks (2) 
Topic archived. No new replies allowed.