#include <iostream>
usingnamespace std;
class Base { };
class AClass: public Base { };
class BClass: public Base { };
int main()
{
constint num=4;
Base * array[num];
AClass obj0;
array[0] = &obj0;
AClass obj1;
array[1] = &obj1;
AClass obj2;
array[2] = &obj2;
AClass obj3;
array[3] = &obj3;
for (int i=0; i<num; i++)
{
cout << "addr = " << array[i] << endl;
}
return 0;
}
I want to the code to instantiate the array in a way that if num changes, the user only needs to change num in the config.h file and recompiled.
Here is one idea that did not work:
1 2 3 4 5 6 7
for (int i=0; i<num; )
{
AClass objA;
array[i++] = &objA; // same addr on each iteration
AClass objB;
array[i++] = &objB; // same addr on each iteration
}