Aug 25, 2010

The new C++

Phew, I finished getting a Win Form App, included with a user-built class, running. It was quite a pain, especially if you have already got used to the comfort of Command Line Interface (CLI) used for studying C++ or GUI development with C#. C++ is somewhere in between, and well, a  bit hard to catch up

After spending around 5 hours on this, I found some of the following interesting features:
  • In C++, whenever you create a Win Form project (all I know beside Win32 Console right now), you will automatically have a stdafx.h & stdafx.cpp to do the standard #include operation for all the code file. Any special #include can be implemented in stdafx.h
    Sample:


    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently

    #pragma once

    // TODO: reference additional headers your program requires here
    //#include <string>
    #include "SimpleException.h"
  • Class ends with ";" (semi-colon). I forgot about that and wasted a few hours.
  • C++ can also have garbage collection like C#. Fot that, you must follow a special syntax called "managed C++":


    ref class SimpleException
        {
        public:
        //constructor
            //default
            SimpleException(String^);
        //accessor
            //get error message
            String ^GetError();
        private:
            String ^_errMsg;
        }
  • In managed C++, instead of using value typed variables, the code uses handler (which is still different from unmanaged C++), something like pointer. However, instead of using the asterisk character (*), we use  the caret character '^' to denote these managed objects. Plus when declaring structures & classes, we also use the "ref" keyword (since all the structure / classes are "handler" by definition)
Okay, this is the code for referencing:
Download

No comments:

Post a Comment