在圖書館裏我使用它與不同的簽名功能如下如何開機運行時間數據類型
void Func(int* ptr);
void Func(float* ptr);
void Func(double* ptr);
我的功能需要決定哪些功能取決於輸入標誌iSwitch如下呼籲:
void test(int iSwitch, void* ptr1)
{
switch (iSwitch)
{
case 0:
Func((int*) ptr1);
break;
case 1:
Func((float*) ptr1);
break;
case 3:
Func((double*) ptr1);
break;
}
}
應該沒問題。但是我不想將Func放在開關塊內。我希望能夠以某種方式在開關塊中首先切換指針的類型,然後只有1個Func,之後ptr1被動態地轉換爲相應的類型。
具體而言,我想要的東西如下:
void test(int iSwitch, void* ptr1)
{
switch(iSwitch)
{
// I want to be able to somehow switch type of pointer (say DummyDataType) here depending iSwitch
}
Func((DummyDataType) ptr1);
}
能否請你的意見,我該怎麼辦呢?
謝謝!
聽起來非常像模板的用途。或指針數組。 –
'boost :: variant'可能會幫助你。 – chris
在編譯時必須選擇被調用的過載(因此在交換機中需要這三個調用)。 – Pixelchemist