Changing Namespace at Runtime

  • Thread starter Kasper Heftholm Kristensen
  • Start date
K

Kasper Heftholm Kristensen

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;
}
 
A

Andrew Koenig

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.
 
J

Jesper Madsen

Kasper Heftholm Kristensen said:
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
 
K

Kasper Heftholm Kristensen

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
 
K

Kasper Heftholm Kristensen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top