扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:许嵩罡编译 来源:VCKBASE 2007年11月16日
关键字:
interface class IEnumerator { property typeParameter Current { typeParameter get(); } bool MoveNext(); void Reset(); } |
Figure 1 Indicating typeParameter Generic Keyword generic <typename typeParameter> interface class IEnumerator { property typeParameter Current { typeParameter get(); } ... }; Template Keyword template <typename typeParameter> interface class IEnumerator { property typeParameter Current { typeParameter get(); } ... }; |
Figure 2 Template and Generic Stack Declaration C++/CLI Template Stack Declaration #include <cliext/vector> using namespace cliext; template <class elemType> ref class tStack { // the CLI vector is a reference class … vector<elemType> ^m_stack; int top; public: tStack(); elemType pop(); void push( elemType et ); ... }; C++/CLI Generic Stack Declaration using namespace System::Collections::Generic generic <class elemType> public ref class gStack { List<elemType> ^m_stack; int top; public: gStack(); elemType pop(); void push( elemType et ); ... }; |
Figure 3 Instantiating the Template Stack void demo_template_Stack() { // an int value type argument... tStack<int>^ is = gcnew tStack<int>( 10 ); for ( int ix = 0; ix < 10; ix++ ) is->push( ix*2 ); int elem_cnt = is->size(); for ( int ix = 0; ix < elem_cnt; ++ix ) Console::WriteLine( "({0}) {1}", ix+1, is->pop()); // a String^ reference type argument... tStack<String^> ^ss = gcnew tStack<String^>( 10 ); ss->push( "Pooh" ); ss->push( "Piglet" ); ss->push( "Rabbit" ); ss->push( "Eeyore" ); elem_cnt = ss->size(); for ( int ix = 0; ix < elem_cnt; ++ix ) Console::WriteLine( "({0}) {1}", ix+1, ss->pop()); } |
Figure 4 Instantiating the Generic Stack void demo_generic_Stack() { // an int value type argument... gStack<int>^ is = gcnew gStack<int>( 10 ); for ( int ix = 0; ix < 10; ix++ ) is->push( ix*2 ); int elem_cnt = is->size(); for ( int ix = 0; ix < elem_cnt; ++ix ) Console::WriteLine( "({0}) {1}", ix+1, is->pop()); // a String^ reference type argument... gStack<String^> ^ss = gcnew gStack<String^>( 10 ); ss->push( "Pooh" ); ss->push( "Piglet" ); ss->push( "Rabbit" ); ss->push( "Eeyore" ); elem_cnt = ss->size(); for ( int ix = 0; ix < elem_cnt; ++ix ) Console::WriteLine( "({0}) {1}", ix+1, ss->pop()); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者