QUESTION
I'm currently teaching myself C++. I'm very proficient at C#, and was wondering which common practices in C# can lead to difficulties in C++, and what a C++ programmer should do instead.
ANSWER
-
The first and foremost, you must learn RAII: Resource Acquisition Is Initialization
-
Make sure if you use
new, then don't forget to usedeletein your program. Don't mixnewwithmalloc, anddeletewithfree.newandmallocare not interchangeable, nor aredeleteandfree. As in C#, you don't write destructor (usually), so in C++ you specifically need to learn how to use destructor effectively to free memory, and other resources! -
Must learn what is sequence points, undefined-behavior, unspecified-behavior, implementation-defined behavior in C++. As C# doesn't have these concepts, that means you've never heard these terms while programming in C#, so it's very very very likely that you'll write C++ code (such as
a[++i]= i,i+=++i) whose behavior is not well-defined. So I would recommend you to read these topics: - Read items from Effective C++ Series by Scott Meyers. After them, read also Exceptional C++ Series by Herb Sutter.
PS: Since I talked about new, delete, raw memory etc, then let me add an interesting difference between C++ and C# :
In C++, this is a pointer, while in C# this is a reference!