小编use*_*989的帖子

条件变量声明

我来自Python,我在管理c ++类型方面遇到了一些问题.在Python中,我可以这样做:

if condition_is_true:
    x=A()
else:
    x=B()
Run Code Online (Sandbox Code Playgroud)

在程序的其余部分,我可以使用x而不关心x的类型,因为我使用具有相同名称和参数的方法和成员变量(不必使A和B具有相同的基类).现在在我的C++代码中,A类型对应于

typedef map<long, C1> TP1;
Run Code Online (Sandbox Code Playgroud)

和B到:

typedef map<long, C2> TP2;
Run Code Online (Sandbox Code Playgroud)

哪里:

typedef struct C1
{
    char* code;
    char* descr;
    int x;
...
}
Run Code Online (Sandbox Code Playgroud)

typedef struct C2
{
    char* code;
    char* other;
    int x;
...
}
Run Code Online (Sandbox Code Playgroud)

C1和C2有类似的成员,在我所说的代码部分中我只需要使用具有相同名称/类型的代码

我想做的事情如下:

if (condition==true)
{
    TP1 x;
}
else
{
    TP2 x;
}
Run Code Online (Sandbox Code Playgroud)

c ++中的正确​​方法是什么?

提前致谢

c++ types declaration

5
推荐指数
2
解决办法
6769
查看次数

标签 统计

c++ ×1

declaration ×1

types ×1