开发者

switch namespace by if condtion

开发者 https://www.devze.com 2023-02-05 15:53 出处:网络
in my C++ program I have several namespaces that contain several pointers with identical names. I then want a function to choose a namespace according to a parameter. I.e. something like:

in my C++ program I have several namespaces that contain several pointers with identical names. I then want a function to choose a namespace according to a parameter. I.e. something like:

#include <iostream>

namespace ns1{
double x[5]={1,2,3,4,5};
}
namespace ns2{
double x[5]={6,7,8,9,10};
}

int main(){
  int b=1;
  if(b==1){
    using namespace ns1;
  }
  if(b==2){
    using namespace ns2;
  }
  std::cout << x[3] << std::endl;
}

However, this doesn't work since the compiler complains that x isn't known in that scope. I guess the problem is that "using namespace ..." is only valid within the if-statement. I think that it should be possible to switch namespaces somehow, but cannot find out how... Do you know how to do this without casting all variable separately?

int main(){
开发者_开发知识库  int b=1;
  double *x;
  if(b==1){
    x = ns1::x;
  }
  if(b==2){
    x = ns2::x;
  }
  std::cout << x[3] << std::endl;
}

Cheers, Pascal


Namespaces are a compile-time feature in C++, you can't manipulate them dynamically in runtime. The best advice I could give you is try a different approach - namespaces are probably not meant to do what you want to do. So ask SO what you really want to do and you'll likely get a good answer without bending the system.


The namespace is used at compilation time. The if state ist executed at run time.

You can't switch the compiler behavior at run time.


I fail to see why you don't want this:

double* dispatch(int b)
{
    switch(b)
    {
        case 1: return ns1::x;
        case 2: return ns2::x;
        default: return 0;
    }
}

int main()
{
    int b=1;
    std::cout << dispatch(b)[3] << std::endl;
}

If this is going to grow, consider using classes and polymorphism instead of namespaces.


In the simplest state you have "static" data but want to decide which piece of static data to use.

struct space
{
  double x[5];
};

space space1 = space{ {1,2,3,4,5} };
space space2 = space{ {6,7,8,9,10} };

int main( int argc, char * argv[] )
{
   space * s;
   if( argc == 1 )
       s = &space1;
   else
       s = &space2;
}

In reality of course your spaces will more than just one member and you will populate them in a different manner but it is one way you can chose which instance you use.

You might use polymorphism to implement functions but it depends a lot on what you need it for.


I don't believe namespaces are something that is determined during run time... therefore you won't be able to set it up as such.

A quick google of "changing namespace at runtime" returned http://www.velocityreviews.com/forums/t288720-changing-namespace-at-runtime.html

Hope that helps.


the using namespace directives basically is telling the compiler to "try to use this when resolving symbols", which doesn't happen at runtime.

I don't see any problem with the second approach....


As mentioned before it won't work... I would suggest that you use polymorphism if you have many variables that you want to put in your container class to achieve the intended result:

// Header file
class s
{
  public:
    double x[5];
}

class s1: public s 
{
  public:
    s1() { /* Assign x here */ };        
}

class s2: public s
{
  public:
    s2() { /* Assign x here */ };
}

// CPP file
int main()
{
  int b = 1;
  s* sptr;

  if (b == 1)
    sptr = (s*)new s1();
  else
    sptr = (s*)new s2();

  std::cout << sptr->x[3] << std::endl;
  delete sptr;
} 

If you only have a few variables you could use pointers.

int main()
{
  int b = 1;
  double *px;
  double s1x = {1, 2, 3, 4, 5};
  double s2x = {6, 7, 8, 9, 10};

  if (b == 1)
    px = &s1x;
  else
    px = &s2x;

  std::cout << px[3] << std::endl;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号