template <class elemType, int size>
public ref class tStack
{
array<elemType> ^m_stack;
int top;
public:
tStack() : top( 0 )
{
m_stack = gcnew array<elemType>( size );
}
};
// 带有默认值的模板声明
template <class elemType, int size = 1024>
public ref class FixedSizeStack {};
// 最多128个字符串实例的堆栈
FixedSizeState<String^, 128> ^tbs = gcnew FixedSizeStack<String^, 128>;
// 最多1024个字符串实例的堆栈
FixedSizeStack<String^> ^tbs = gcnew FixedSizeStack<String^>;
// ISO-C++名字空间std中的默认类型参数值示例
{
template <class T, class Container = deque<T> >
class queue;
template <class T, class Allocator = allocator<T> >
class vector;
// ...
}
// 带有默认的元素类型的模板声明
template <class elemType=String^, int size=1024>
public ref class tStack {};
typedef void (*handler)( ... array<Object^>^ );
template <class elemType, int size, handler cback >
public ref class tStack {};
void defaultHandler( ... array<Object^>^ ){ ... }
template < class elemType,
int size = 1024,
handler cback = &defaultHandler >
public ref class tStack {};
void demonstration()
{
// 默认的大小和处理程序
tStack<String^> ^ts1 = nullptr;
// 默认的处理程序
tStack<String^, 128> ^ts2 = gcnew tStack<String^, 128>;
// 重载所有的三个参数
tStack<String^, 512, &yourHandler> ^ts3;
}
// template模板参数
template <template <class T> class arena, class arenaType>
class Editor {
arena<arenaType> m_arena;
// ...
};
// 模板缓冲区类
template <class elemType>
public ref class tBuffer {};
void f()
{
Editor<tBuffer,String^> ^textEditor;
Editor<tBuffer,char> ^blitEditor;
// ...
}
类型参数约束template <class T>
ref class Demonstration {
int method() {
typename T::A *aObj;
// ...
}
};
int demoMethod()
{
Demonstration<int> ^demi =
gcnew Demonstration<int>( 1024 );
return dm->method();
}
error C2825: ’T’: must be a class or namespace when followed by ’::’
template <class elemType, int size=1024>
ref class Container
{
array<elemType> ^m_buf;
int next;
public:
bool search( elemType et )
{
for each ( elemType e in m_buf )
if ( et == e )
return true;
return false;
}
Container()
{
m_buf = gcnew array<elemType>(size);
next = 0;
}
void add( elemType et )
{
if ( next >= size )
throw gcnew Exception;
m_buf[ next++ ] = et;
}
elemType get( int ix )
{
if ( ix < next )
return m_buf[ ix ];
throw gcnew Exception;
}
// ...
};
template <class elemType, int size=1024>
ref class Container
{
// 其它的都相同 ...
// 这是一个函数成员模板...
// 它可以同时引用包含的类参数和自有参数...
template <class Comparer>
bool search( elemType et, Comparer comp )
{
for each ( elemType e in m_buf )
if ( comp( et, e ) )
return true;
return false;
}
// ...
};
class EqualGuy {
public:
bool Operator()( String^ s1, String^ s2 )
{
return s1->CompareTo( s2 ) == 0;
}
};
int main()
{
Container<String^> ^sxc = gcnew Container<String^>;
sxc->add( "Pooh" );
sxc->add( "Piglet" );
// 成员模板搜索 ...
if ( sxc->search( "Pooh", EqualGuy() ) )
Console::WriteLine( "found" );
else Console::WriteLine( "not found" );
// 传统的等于搜索 ...
if ( sxc->search( "Pooh" ) )
Console::WriteLine( "found" );
else Console::WriteLine( "not found" );
}
generic <class elemType>
public ref class Container
{
array<elemType> ^m_buf;
int next;
int size;
public:
bool search( elemType et )
{
for each ( elemType e in m_buf )
if ( et->CompareTo( e ))
return true;
return false;
}
Container( int sz )
{
m_buf = gcnew array<elemType>(size = sz);
next = 0;
}
// add() 和 get() 是相同的 ...
};
error C2039: ’CompareTo’ : is not a member of ’System::Object’
error C2676: binary ’==’ : ’elemType’ does not define this operator
or a conversion to a type acceptable to the PRedefined operator
generic <class elemType>
where elemType : IComparable
public ref class Container
{
// 类的主体没有改变 ...
};
int main()
{
// 正确的:String和int实现了IComparable
Container<String^> ^sc;
Container<int> ^ic;
//错误的:StringBuilder没有实现IComparable
Container<StringBuilder^> ^sbc;
}
generic <class T1, class T2, class T3>
where T1 : IComparable, ICloneable, Image
where T2 : IComparable, ICloneable, Image
where T3 : ISerializable, CompositeImage
public ref class Compositor
{
// ...
};
generic <class T1, class T2, class T3>
// 错误的:同一个参数不答应有两个条目
where T1 : IComparable, ICloneable
where T1 : Image
public ref class Compositor
{
// ...
};
generic <class T1, class T2>
where T1 : IComparable<T1>
where T2 : IComparable<T2>
public ref class Compositor
{
// ...
};
generic <class T1, class T2>
public ref class BlackWhite_Compositor : Compositor
{
// ...
};
generic <class T1, class T2>
where T1 : IComparable<T1>
where T2 : IComparable<T2>
public ref class BlackWhite_Compositor : Compositor
{
// ...
};
新闻热点
疑难解答