Introduction
When you use managed C++, your code is handled by the common language runtime (CLR). That means things like garbage collection and interoperability are done for you by the CLR. I have given below a well-commented simple program to get you guys started. I am not sure how it's gonna work if you use VS.Net. I used notepad to create the cpp file and compiled it using cl.exe with the /clr switch. I thought I'd better get a hang of things before I use the VS.Net App Wizard to generate managed C++ code for me.
Anyhow good luck....
The Program
#using
using namespace System;
//__gc means this is a garbage collected class
//we don't have to delete the instances
public __gc class abc
{
public:
String *s1;
//overloaded member functions
void Greet ( String* s ) ;
void Greet( ) ;
//overloaded constructors
abc( ) ;
abc ( int ) ;
} ;
void main( )
{
Console::WriteLine ( "Hello, what's up? " ) ;
abc *a1= new abc( ) ;
a1->Greet( ) ;
a1->Greet ( "nish" ) ;
abc *a2 = new abc ( 5 ) ;
//Now lets have some fun with pointers
String* s ; //Create a string s on the CLR heap
String* &x=s ; //Create a string x that references s
String* *z=&s ; //Create a string pointer and point it to s
//Well, basically now we have x,z,s all pointing to the same memory :-)
s = "This is fun" ;
Console::WriteLine ( x ) ;
Console::WriteLine ( s ) ;
Console::WriteLine ( *z ) ;
x="More fun";
Console::WriteLine ( x ) ;
Console::WriteLine ( s ) ;
Console::WriteLine ( *z ) ;
*z="Getting really cool now";
Console::WriteLine ( x ) ;
Console::WriteLine ( s ) ;
Console::WriteLine ( *z ) ;
}
abc::abc( )
{
s1 = "Kamran" ;
}
abc::abc ( int i )
{
for ( int j = 0 ; j <>
Console::WriteLine ( "Managed C++ is more fun than C#" ) ;
}
void abc::Greet ( String* s )
{
//I tried %s instead of {0}. Won't work :-)
Console::WriteLine("Hello {0}",s);
}
void abc::Greet( )
{
Console::WriteLine ( "Hello {0}", s1 );
}
Output
D:\test\mc++>hello
Hello, what's up?
Hello Kamran
Hello nish
Managed C++ is more fun than C#
Managed C++ is more fun than C#
Managed C++ is more fun than C#
Managed C++ is more fun than C#
Managed C++ is more fun than C#
This is fun
This is fun
This is fun
More fun
More fun
More fun
Getting really cool now
Getting really cool now
Getting really cool now