Declaring classes in C++/CLI

Declaring classes in C++/CLI follows a much more coherent syntax than we had in Managed C++. On a side-note, the old syntax is nowadays referred to as Managed C++ and the new syntax is referred to as C++/CLI; some people do say old-managed syntax and new-managed syntax, but I think that’s just darn confusing, and prefer the former way of referring to the two syntaxes. In C++/CLI, CLR types are prefixed with an adjective that describes the type, and the adjective is left blank for native classes and structs. Examples are :-

  • class Native {...};
  • struct Native {...};
  • ref class ARefClass {...};
  • ref struct BRefClass {...};
  • value class CValClass {...};
  • interface class ISomeInterface {...};

Let’s see some example code :-

value class Student
{
public:
    String^ strStudentname;
};

interface class IHello
{
    void SayHello(Student student);
};

ref class Hello : public IHello
{
public:
    Hello()
    {
        Console::WriteLine("Hello from C++/CLI");
    }
    void SayHello(Student student)
    {
        Console::WriteLine("Hello {0}",
            student.strStudentname);
    }
};

Okay, so we have a value class Student, we have an interface IHello that defines a method that takes a Student argument, and then a class Hello that implements the IHello interface. Now let’s see some code to put these classes to use :-

void _tmain()
{
    Hello^ h = gcnew Hello();
    Student s;
    s.strStudentname = "Nish";
    h->SayHello(s);
}

Hmmm, those of you who haven’t seen the new syntax will be feeling a bit puzzled by the ^ symbol and the gcnew keyword. The ^ (called the cap symbol) punctuator represents a handle just as a * represents a pointer. According to the CLI specification a handle is a managed object reference. Handles are the new-syntax equivalent of __gc pointers in the MC++ syntax. Handles are not to be confused with pointers and are totally different in nature from pointers. Handles don’t really point to any specific memory location and are essentially tracking references to a managed object on the CLR heap that might keep changing physical locations based on GC cycles or heap compactions.

Just as new is used to instantiate native C++ objects, gcnew is used to instantiate CLR objects. gcnew returns a handle to a managed type that would have been allocated on the garbage collected CLR heap. The good thing about gcnew is that it allows us to easily differentiate between managed and unmanaged instantiations.

Anyway there’s nothing peculiar about our little program, and on compiling and running we get the expected output :-

Hello from C++/CLI
Hello Nish

You will need to compile with the /clr switch (which now defaults to /clr:newSyntax) and you won’t have to add a reference to mscorlib.dll as it is implicitly referenced.

Advertisements

8 thoughts on “Declaring classes in C++/CLI

  1. Nish,

    If you consider throwing a lot of code, I would strongly advise using an html colorizer, there are a couple out there, that’s so much nicer than standard pre code sections.

    Neat look by the way.

  2. Hey Shog

    Blogging is kinda like using XP. All the 2K users said “yuck” before they installed XP, but afterwards they fell in love with it, didn’t they?

    Nish 🙂

  3. Live code colorizing is not an obligatory thing, you could copy/paste the code snippet in a separate colorizer and get the resulting html. The only thing you need is to make sure to include the css styles in your web page.
    That said, I’ll check out whether there’s a live PHP colorizer out there, and let you know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s