使用变量确定类或命名空间的类型 好的,所以我有一个聪明的想法,使用命名空间和if语句来改变变量的结果,在PHP中,我想我可以使用连接,但是,到目前为止,C++似乎没有连接。p>

使用变量确定类或命名空间的类型 好的,所以我有一个聪明的想法,使用命名空间和if语句来改变变量的结果,在PHP中,我想我可以使用连接,但是,到目前为止,C++似乎没有连接。p>,c++,namespaces,C++,Namespaces,那么有没有办法让这个代码正常工作呢 namespace Blue { int seven = 7; int eight = 8; } namespace Red { int seven = 1; int eight = 2; } 这是主cpp文件 int choice; cin >> choice; if(choice == 1) { char A[] = "Blue"; }else if(choice == 2){ char A[] = "Re

那么有没有办法让这个代码正常工作呢

namespace Blue
{

int seven = 7;
int eight = 8;


}

namespace Red
{

int seven = 1;
int eight = 2;

}
这是主cpp文件

int choice;

cin >> choice;

if(choice == 1)
{
    char A[] = "Blue";

}else if(choice == 2){

    char A[] = "Red";

}

cout << A::seven << endl;

cout << A::eight << endl;
int选择;
cin>>选择;
如果(选项==1)
{
字符A[]=“蓝色”;
}else if(选项==2){
字符A[]=“红色”;
}
cout这并没有解决名称空间问题,但是由于您提到了字符串连接

要在C++中连接字符串,请执行以下操作:

string a = "Hello, ";
string b = "World!";
string c = a + b;
我不太清楚你问题中的代码不起作用是什么意思。您在单独的行上输出两个字符串(
endl
相当于
“\n”
+刷新流)

编辑

我认为问题在于您试图使用字符数组。虽然这些技术上是字符串,但它们不是C++的字符串处理方式。如果您想要像C一样处理字符串,请使用C标准库strcat(或者最好是编译器可能附带的更安全的版本)。

这并没有解决名称空间问题,但是因为您提到了字符串连接

要在C++中连接字符串,请执行以下操作:

string a = "Hello, ";
string b = "World!";
string c = a + b;
我不太清楚你问题中的代码不起作用是什么意思。您在单独的行上输出两个字符串(
endl
相当于
“\n”
+刷新流)

编辑


我认为问题在于您试图使用字符数组。虽然这些技术上是字符串,但它们不是C++的字符串处理方式。如果你想做C++之类的字符串,使用C标准库Strut(或者最好是编译器可能使用的更安全的版本)。但是,您可以使用std::map获得类似的结果:

std::map<std::string, int> Blue;
Blue["seven"] = 7;
Blue["eight"] = 8;
assert(Blue["seven"] == 7);
std::地图蓝色;
蓝色[“七”]=7;
蓝色[“八”]=8;
断言(蓝色[“七”]==7);

<代码> > p>你不能直接在C++中这样做。但是,您可以使用std::map获得类似的结果:

std::map<std::string, int> Blue;
Blue["seven"] = 7;
Blue["eight"] = 8;
assert(Blue["seven"] == 7);
std::地图蓝色;
蓝色[“七”]=7;
蓝色[“八”]=8;
断言(蓝色[“七”]==7);
您无法决定在运行时使用哪个命名空间

这类事情只在编译时进行。

您无法决定在运行时使用哪个名称空间


这类事情只在编译时进行。

不可能分配类或名称空间并使名称查找的结果依赖于此。但是您可以指定变量的地址。所以在你的例子中,你可以说

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;
数组nums;
如果(选项==1)
{
数组blues={{&Blue::seven,&Blue::seek};
nums=蓝色;
}else if(选项==2){
数组reds={{&Red::seven,&Red::seek};
nums=红色;
}

cout不可能分配类或名称空间并使名称查找的结果依赖于此。但是您可以指定变量的地址。所以在你的例子中,你可以说

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;
数组nums;
如果(选项==1)
{
数组blues={{&Blue::seven,&Blue::seek};
nums=蓝色;
}else if(选项==2){
数组reds={{&Red::seven,&Red::seek};
nums=红色;
}

coutNo这将不起作用,因为命名空间是在编译时计算的,并且直到运行时才知道的。我在项目中经常遇到这种情况。我与之接口的语言中有一个动态类型,它在运行时已知,C++使用编译时的分辨率来进行打字。通常,您可以使用case语句来解决这个问题,该语句调用专门的模板函数

    enum Choice
    {
        blue,
        red
    }

    template <Choice c>
    struct Option
    {
        enum {seven=0};
        enum {eight=0};
    };

    template <>
    struct Option<blue>
    {
        enum {seven=7};
        enum {eight=8};
    }

    template <>
    struct Option<red>
    {
        enum {seven=1};
        enum {eight=2};
    }

    ...

    switch (choice)
    {
    case red:
        cout << Option<red>::seven << endl;
        cout << Option<red>::eight << endl;
    case blue:
        cout << Option<blue>::seven << endl;
        cout << Option<blue>::eight << endl;
    }
枚举选择
{
蓝色
红色
}
模板
结构选项
{
枚举{seven=0};
枚举{8=0};
};
模板
结构选项
{
枚举{seven=7};
枚举{8=8};
}
模板
结构选项
{
枚举{seven=1};
枚举{8=2};
}
...
开关(选择)
{
红色表壳:

CUT< P>不,因为在编译时命名空间被评估,并且直到运行时,A的内容才知道。我与我的项目一起运行了很多。我与之接口的语言中有一个动态类型,在运行时已知,C++使用编译时间分辨率来进行打字。通常你会用一个case语句来解决这个问题。它调用专门的模板函数

    enum Choice
    {
        blue,
        red
    }

    template <Choice c>
    struct Option
    {
        enum {seven=0};
        enum {eight=0};
    };

    template <>
    struct Option<blue>
    {
        enum {seven=7};
        enum {eight=8};
    }

    template <>
    struct Option<red>
    {
        enum {seven=1};
        enum {eight=2};
    }

    ...

    switch (choice)
    {
    case red:
        cout << Option<red>::seven << endl;
        cout << Option<red>::eight << endl;
    case blue:
        cout << Option<blue>::seven << endl;
        cout << Option<blue>::eight << endl;
    }
枚举选择
{
蓝色
红色
}
模板
结构选项
{
枚举{seven=0};
枚举{8=0};
};
模板
结构选项
{
枚举{seven=7};
枚举{8=8};
}
模板
结构选项
{
枚举{seven=1};
枚举{8=2};
}
...
开关(选择)
{
红色表壳:

我不认为有办法做到这一点,如果有的话,这可能是个坏主意。你通常会用C++来完成C++中的多态行为,例如:

class Color {
  virtual int seven() = 0;
  virtual int eight() = 0;
};
class Blue : public Color {
  int seven() { return 7; }
  int eight() { return 8; }
};
class Red : public Color {
  int seven() { return 1; }
  int eight() { return 2; }
};
然后:

int choice;
Color* color;

cin << choice;
if (choice == 1)
  color = new Blue;
else
  color = new Red;

cout << color->seven() << endl;
cout << color->eight() << endl;

delete color;
int选择;
颜色*颜色;

CIN < P>我认为没有办法做到这一点,如果有的话,这可能是个坏主意。通常,C++会使用多态性来完成这种行为,例如:

class Color {
  virtual int seven() = 0;
  virtual int eight() = 0;
};
class Blue : public Color {
  int seven() { return 7; }
  int eight() { return 8; }
};
class Red : public Color {
  int seven() { return 1; }
  int eight() { return 2; }
};
然后:

int choice;
Color* color;

cin << choice;
if (choice == 1)
  color = new Blue;
else
  color = new Red;

cout << color->seven() << endl;
cout << color->eight() << endl;

delete color;
int选择;
颜色*颜色;

cin在我看来,您似乎并不真正关心您提出的问题,而是真正想知道,“名称空间有什么意义?”

名称空间主要用于将具有相同名称的类(和其他数据元素)彼此分离

在C++被标准化之前,没有STD::string,但是每第三方库都有它自己的。因此,如果你想同时为WIDGeWorks库提供一些东西,并且对于其他东西来说,CurrObjor库,如果你只写了:

,你会遇到问题。
#include "widgetworx.h"
#include "coolobjects.h"
因为两者都将定义一个名为
string
的类。因此,标准化委员会意识到,如果他们添加自己的
string
,现在将有三个。解决方案是名称空间