我寫了一個類,它需要兩個組成部分,一個隨機類類型T和一個整數,我實現它像以下: 在Test.h,如:如何用C++中的泛型類創建對象?
template <class A, int B>class Test { // two components,
private:
A first;
int second;
public:
Test();
Test (A,int);
}
在Test.cpp的我所做的:
template <class T,int i> Test<T,i>::Test() {}
template <class A,int i>Test<A,i>::Test(T a, int b):first(a) {second=b;}
但在主要功能:
Test<int, int > T1; //It can not be passed
Test<int, 4> T2; //It can not be passed
int x = 8;
Test<int, x> T3 (3,4);// can not be passed
我怎樣才能申報對象instanc e從上面的泛型類?
感嘆。 http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15。 – 2012-04-05 00:02:14
正確的術語是「類模板」。在這種情況下,「通用」一詞有點危險,因爲它在其他語言中有着非常不同的含義。 – 2012-04-05 00:02:57
我如何從Test類聲明一個對象?這是個問題。夥計,我需要它....請 – ToBeGeek 2012-04-05 00:19:09