Answered C# to C++

  • Monday, December 12, 2011 4:30 PM
     
     

    Just wondering if you the classes work the same in C++, for example in C# I would create a class, declare a variable of that type and then use the new key word to instantiate the class.  I would also use properties (public) in C# for the interface.  Would C++ be the same?  Also does C++ support exeption processing try, catch and is the syntax the same?

    thanks Paul.

Answers

  • Monday, December 12, 2011 4:41 PM
     
     Answered

    Il 12/12/2011 17:30, svt44cobra66 ha scritto:

    Just wondering if you the classes work the same in C++, for example in C# I would create a class, declare a variable of that type and then use the new key word to instantiate the class.  I would also use properties (public) in C# for the interface.  Would C++ be the same?

    In C++ you can use stack semantic to create instances of classes, without operator new.

    If there is a need to allocate instances of classes on the heap, you can use "new", but it's better to use smart pointers instead of raw pointers, e.g. shared_ptr/make_shared.

    When instances go out of scope (or, in case of reference counting, when the ref count reaches zero), the class destructor is called: this is the time to perform resource cleanup.

    (Note that C# Dispose pattern is different from C++ destructors.)

    Also does C++ support exeption processing try, catch and is the syntax the same?

    C++ does support exceptions with try/catch. There is no finally block, because resources are released in class destructors.

    Giovanni

  • Monday, December 12, 2011 4:46 PM
     
     Answered

    C++/CLI is very similar, expect for syntax details of course.  A good reference is "C++/CLI in Action" by Nishant Sivakumar.

    However, if you don't want to join the 47 developers currently actively using C++/CLI, then you probably want native C++.  In this case, there is very little similarity.  Some of the concepts are the same, but the syntax is quite different.  You don't have 'interfaces' and 'properties', but pure virtual methods and accessor methods.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)

All Replies

  • Monday, December 12, 2011 4:41 PM
     
     Answered

    Il 12/12/2011 17:30, svt44cobra66 ha scritto:

    Just wondering if you the classes work the same in C++, for example in C# I would create a class, declare a variable of that type and then use the new key word to instantiate the class.  I would also use properties (public) in C# for the interface.  Would C++ be the same?

    In C++ you can use stack semantic to create instances of classes, without operator new.

    If there is a need to allocate instances of classes on the heap, you can use "new", but it's better to use smart pointers instead of raw pointers, e.g. shared_ptr/make_shared.

    When instances go out of scope (or, in case of reference counting, when the ref count reaches zero), the class destructor is called: this is the time to perform resource cleanup.

    (Note that C# Dispose pattern is different from C++ destructors.)

    Also does C++ support exeption processing try, catch and is the syntax the same?

    C++ does support exceptions with try/catch. There is no finally block, because resources are released in class destructors.

    Giovanni

  • Monday, December 12, 2011 4:46 PM
     
     Answered

    C++/CLI is very similar, expect for syntax details of course.  A good reference is "C++/CLI in Action" by Nishant Sivakumar.

    However, if you don't want to join the 47 developers currently actively using C++/CLI, then you probably want native C++.  In this case, there is very little similarity.  Some of the concepts are the same, but the syntax is quite different.  You don't have 'interfaces' and 'properties', but pure virtual methods and accessor methods.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
  • Monday, December 12, 2011 5:25 PM
     
     
    ok thanks for the responses.