The new C++/CLI syntax in VC++ 2005


Using a newer C++ compiler build than the one in VC++ 2005 Beta 1, I wanted to try what Stan described on String Literals are now a Trivial Conversion to String and a couple of other new features:


 



using namespace System ;


using namespace stdcli::language ;


 


public ref class B


{


   public:


      B()  { Console::WriteLine( “B::B()” ) ; }


      ~B() { Console::WriteLine( “B::~B() a.k.a. Dispose()” ) ; }


 


   protected:


      !B() { Console::WriteLine( “B::!B() a.k.a. Finalize()” ) ; }


} ;


 


public ref class R1 : B


{


   public:


      R1( short ) { Console::WriteLine( “R1::R1()” ) ; }


      ~R1()       { Console::WriteLine( “R1::~R1() a.k.a. Dispose()” ) ; }


 


      void f( const char * )    { Console::WriteLine( “f( const char * )” ) ; }


      void f( const wchar_t * ) { Console::WriteLine( “f( const wchar_t * )” ) ; }


      void f( String ^ )        { Console::WriteLine( “f( String ^ )” ) ; }


 


   protected:


      !R1()  { Console::WriteLine( “R1::!R1() a.k.a. Finalize()” ) ; }


} ;


 


public ref class R2 : B


{


   public:


      R2( short ) { Console::WriteLine( “R2::R2()” ) ; }


 


   protected:


      !R2()  { Console::WriteLine( “R2::!R2() a.k.a. Finalize()” ) ; }


} ;


 


 


void wmain()


{


   R1 r1(32767) ;


   R2 r2(-32768) ;


   r1.f( “ANSI” ) ;


   r1.f( static_cast<const char *>(“ANSI”) ) ;


   r1.f( L”Unicode” ) ;


   r1.f( static_cast<const wchar_t *>(L”Unicode”) ) ;


}


 


 


The output is:


 



B::B()


R1::R1()


B::B()


R2::R2()


f( String ^ )


f( const char * )


f( String ^ )


f( const wchar_t * )


B::~B() a.k.a. Dispose()


R1::~R1() a.k.a. Dispose()


B::~B() a.k.a. Dispose()


 


Note that R2::!R2() is not called as System.GC::SuppressFinalize() was called in B::~B(): I would have liked to see a warning when compiling the source.


Something like:


B, the base class for R2 has a destructor (Dispose()) and R2 does not: R2:!R2() (R2::Finalize()) might not be called.


 


Why might? Here is the output I got by adding System::Environment::Exit(0) as the last line in wmain():


 



B::B()


R1::R1()


B::B()


R2::R2()


f( String ^ )


f( const char * )


f( String ^ )


f( const wchar_t * )


R2::!R2() a.k.a. Finalize()


B::!B() a.k.a. Finalize()


R1::!R1() a.k.a. Finalize()


B::!B() a.k.a. Finalize()


 

Comments (1)

Skip to main content