"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com...
Hi.
I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.
The code below will not compile, but I hope it gives you an idea of
what I'm trying to accomplish.
I guess it has to do with how namespaces are only defined withing a
block, but I have know idea how to do it in another way.
Cheers,
Kasper Kristensen
// ---------------------- CODE--------------------------
namespace A
{
int test=1;
}
namespace B
{
int test=2;
}
int main(int argc, char* argv[])
{
bool useA=true;
if(useA)
using namespace A;
else
using namespace B;
cout << test << "\n";
return 0;
}
You could come close to this effect using inheritance and classes...
struct testInterface {
virtual int test() = 0;
};
struct testInterfaceA : public testInterface {
virtual int test() { return 1; };
};
struct testInterfaceB : public testInterface {
virtual int test() { return 2; };
};
or you could
{
int test = 0;
bool useA=true;
if (useA)
test = A::test;
else
test = B::test;
cout << test << "\n";
}
If it can be decided on compile-time, it would be much easier to solve this.
Depending on your need, you could have a function pointer, a struct or class
with members, or a pointer to some data, which you can change at runtime..
JM