我写了一个类,它需要两个组成部分,一个随机类类型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