It's not possible to assign classes or namespaces and make the outcome of namelookup dependent on this. But you can assign the address of variables. So in your example, you could say
array<int*, 2> nums;
if(choice == 1)
{
array<int*, 2> blues = {{ &Blue::seven, &Blue::eight }};
nums = blues;
} else if(choice == 2) {
array<int*, 2> reds = {{ &Red::seven, &Red::eight }};
nums = reds;
}
cout << *nums[0] << endl;
cout << *nums[1] << endl;
However in this case you can put the print into each respective if clause, which looks easier to me
if(choice == 1)
{
cout << Blue::seven << endl;
cout << Blue::eight << endl;
} else if(choice == 2) {
cout << Red::seven << endl;
cout << Red::eight << endl;
}
If you accesses are often, you may wish to put the colors into a single data-structure
typedef std::array<int, NumberCount> number_array;
typedef std::array<number_array, ColorCount> color_array;
color_array colors = {{ {{ 7, 8 }}, {{ 1, 2 }} }};
So you could use an index, maybe using enumerations for the color names instead of raw numbers
int arrayIndex = -1;
if(choice == 1)
{
arrayIndex = 0;
} else if(choice == 2) {
arrayIndex = 1;
}
if(arrayIndex != -1) {
cout << colors[arrayIndex][0] << endl;
cout << colors[arrayIndex][1] << endl;
}
This way you can also iterate over the colors (using array's size()
function or its iterator interface). array
is part of TR1, C++0x and Boost.