2015-04-24 54 views
-2

我想弄清楚模板類。 我宣佈了一個模板類,它是模板。但現在我想將該char傳遞給PrintChar類的構造函數。我不想使用模板。但使用構造函數。我該怎麼做?我想扭轉操作。我想用常規的構造函數而不是模板來完成它。我的結構在下面。謝謝。C++使用模板

template<char C> 
class PrintChar : public PrintIt { 
//something... 
}; 

main() 
PrintChar<'*'> pstuff_star(min,max); 
PrintChar<'~'> pstuff_tilde(min,max); 
+0

你想說你想寫'PrintChar foo('*')'並讓它創建一個'PrintChar <'*'>'? – NathanOliver

回答

1

也許......

class PrintChar : public PrintIt 
{ 
protected: 
    char _ch; //This is char passed to constructor 

public: 
    PrintChar(char ch, /* other parameters */) 
    : _ch(ch) 
    { 
    } 
}; 

int main() 
{ 
    PrintChar pstuff_star('*', min, max); 
    PrintChar pstuff_tilde('~', min, max); 

    //... 
} 

不要過度使用模板。如果你不需要它們 - 不要使用它們。