2013-09-24 136 views
-2

我使用兩個結構模擬「或」&「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?

+2

請添加更多信息。 – noelicus

回答

1

您可以只使用一個工會爲你的兩個不同的結構,然後通過工會:

struct AND_gate { 
    // ... 
}; 

struct OR_gate { 
    // ... 
}; 

union gate { 
    AND_gate and_gate; 
    OR_gate or_gate; 
}; 

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

或者,也許更好的(很難說與現有的有限信息),這聽起來像你的設計可能受益於使用繼承:

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

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

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

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

你可能意味着你如何返回一個結構......在這種情況下,就像這樣:

typedef struct 
{ 
... 
} YourStruct; 

YourStruct *Function(...) 
{ 
    return &GlobalStructureForAndGate; 
}