-2

我使用两种结构来模拟“OR”和“AND”逻辑门。这些结构是相同的。我需要创建一个函数,该函数将这两个结构中的一个作为参数。就像是 :

int myfunc(void *mystruct, unsigned char param)
{
switch (param)
{
case 'o': ... break; //"OR" logic gate struct
case 'a': ... break; //"AND" logic gate struct
}
} 

如何在托管代码 c++/cli 中实现这个想法?

4

2 回答 2

1

You can just use a union for your two different structs and then pass the union:

struct AND_gate {
    // ...
};

struct OR_gate {
    // ...
};

union gate {
    AND_gate and_gate;
    OR_gate or_gate;
};

int myfunc(gate * my_gate, unsigned char param)
{
    // ...
}

Alternatively, and perhaps better (hard to tell with the limited information available), it sounds like your design might benefit from using inheritance:

struct gate {    // parent class
    // ...
};

struct AND_gate: public gate {
    // ...
};

struct OR_gate: public gate {
    // ...
};

int myfunc(gate * my_gate, unsigned char param)
{
    // ...
}
于 2013-09-24T09:56:55.223 回答
0

您可能的意思是如何返回一个结构......在这种情况下,就像这样:

typedef struct
{
...
} YourStruct;

YourStruct *Function(...)
{
    return &GlobalStructureForAndGate;
}
于 2013-09-24T10:05:24.417 回答