[Assorted-commits] SF.net SVN: assorted:[1196] sandbox/trunk/src/cc/tmpl_virt_fn.cc
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-18 18:30:56
|
Revision: 1196 http://assorted.svn.sourceforge.net/assorted/?rev=1196&view=rev Author: yangzhang Date: 2009-02-18 18:30:53 +0000 (Wed, 18 Feb 2009) Log Message: ----------- added demo of templated virtual functions Added Paths: ----------- sandbox/trunk/src/cc/tmpl_virt_fn.cc Added: sandbox/trunk/src/cc/tmpl_virt_fn.cc =================================================================== --- sandbox/trunk/src/cc/tmpl_virt_fn.cc (rev 0) +++ sandbox/trunk/src/cc/tmpl_virt_fn.cc 2009-02-18 18:30:53 UTC (rev 1196) @@ -0,0 +1,27 @@ +// Demo that you can't have templated virtual functions. + +class IWrite { + public: + template< typename tMsg > + virtual void write( const tMsg& msg ) = 0; +}; + +class Write0 { + public: + template< typename tMsg > + virtual void write( const tMsg& msg ) { cout << "Write0" << msg.name(); }; +}; +class Write1 { + public: + template< typename tMsg > + virtual void write( const tMsg& msg ) { cout << msg.name(); }; +}; + +int main( int argc, char** args ) { + // here's your function pointer, neatly wrapped inside a vtable :) + IWrite* pWriter = argc==1? new Write0 : new Write1; + + pWriter->write( CMsgA() ); + pWriter->write( CMsgC() ); + pWriter->write( CMsgB() ); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |