Some differences between C# and C++ - Entry 1

by Jason Haley 29. April 2004 12:27

Up to chapter 8 now in my Visual C++ book. Here are some of the things I have learned so far:

  • You declare the prototypes of your functions and member variables in a header file before you use them (usually anyways).
  • Your function code is in a source file (.cpp) with no class {} wrapping them (that is in the header file).
  • In unmanaged C++ you use the keyword "delete" to clean up your objects when you are done with them.
  • Managed classes are defined with __gc in front of them (ie. __gc class Foo {})
  • In the header file, there is a section for different scoped items like (classes also end with a ";"):
    __gc class Foo {
    public:
     Foo();
     ~Foo();
     void SomeMethod();
    private:
     int SomeID;
    };
    
    Instead of C#:
    class Foo {
     private int _someID;
     public Foo()
     {}
     ~Foo()
     {}
     public void SomeMethod()
     {}
    }
    
  • It seems for strings you use "S" instead of "@" (ie. Console::WriteLine(S"Hello World"); instead of Console.WriteLine(@"Hello World");), only noticed this through usage - not 100% sure if it is a 1-to-1 comparison on this one
  • Unlike VB.Net and C#, C++ can force a Finalize by using the delete keyword, which will then call the destructor and then the Finalize method
  • Syntax for implementing an interface seems a little different - have to add "public"
    __gc class Tester : public IDisposable {
    }
    

Comments (15) | Post RSSRSS comment feed |

Categories:
Tags:

Comments

Comments are closed