0
我有一個模板類方法類似:問題與調用從模板類的方法
template<class T>
static tmpClass<T>* MakeInstance(T value)
{
tmpClass<T> *pointer = new tmpClass<T>(value);
return pointer;
}
我用各種方式來調用這個方法:
方法1:
MakeInstance<int>(val); // This is OK.
方式2:
MakeInstance(int val); // ERROR: Expected '(' for function-style cast or type construction
方式3:
MakeInstance(int (val)); // This is also OK.
方式4:
MakeInstance(int, (val)); // The same issue with way 2
方式5:
MakeInstance((int), (val)); // ERROR: Expect expression with ","
方式6:
MakeInstance((int) val); // This is also OK.
方式7:
MakeInstance<int val>; // ERROR: Expected ">"
那麼方法1,3,6之間有什麼區別嗎?爲什麼我們不能使用「,」來拆分「T」和「value」,僅僅是因爲我們必須嚴格遵循模板?但爲什麼我們在「<>」中也可以有「T」?
您應該在模板上閱讀[好書](http://stackoverflow.com/q/388242/21475)。看來你還不清楚基本的語法。 – Cameron
val是什麼類型的?如果它是'int',那麼你可能不需要1,3,6。只需調用MakeInstance(val);'。 – iammilind
@iammilind它有多種類型,如int,short,long,char,string ...我想你正在談論模板中的默認類型。 –