By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Changing Namespace at Runtime

P: n/a

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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com...
I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.


It's not possible, any more than it is possible to change the type of a
variable at runtime.

Consider:

namespace X { int a; }
namespace Y { double a; }

If you could select a namespace at run time, you could also select at run
time whether the variable "a" has type int or double.
Jul 23 '05 #2

P: n/a

"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
Jul 23 '05 #3

P: n/a
On Tue, 8 Feb 2005 21:04:20 +0100, "Jesper Madsen"
<ba***@mail.stofanet.dk> wrote:

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


Hey, another Dane. Small "world" ;-)

Unfortunately I cannot change the design like this since it's given by
the middleware I use and it uses namespaces for "encapsuling" the
data.

But I have a stupid work around (that doesn't involve me) to do it
without the need for chancing namespace.

But thanks anyway.

Kasper

Jul 23 '05 #4

P: n/a
On Tue, 08 Feb 2005 19:18:31 GMT, "Andrew Koenig" <ar*@acm.org> wrote:
"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com.. .
I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.


It's not possible, any more than it is possible to change the type of a
variable at runtime.

Consider:

namespace X { int a; }
namespace Y { double a; }

If you could select a namespace at run time, you could also select at run
time whether the variable "a" has type int or double.

Yes, I had a strong suspicion it wouldn't be possible, but it was
worth a try, since it would really help me.
Thanks anyway.

Kasper
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.