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.
migration rejected from stackoverflow.com Apr 5 at 16:29
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
closed as too broad by gnat, Nicol Bolas, Robert Harvey♦ Apr 5 at 16:29
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Be wary of
string types in C++, they have grown up from many years of (bitter) experience and there are different types. In C# they're all Unicode (I forget the encoding), but in C++ a
char is usually dependent on the system's code page. For example, US-English Windows uses a code page of Windows 1252 by default for non unicode programs. Code pages are difficult to manage and error prone so use
wchar_t and the STL's
std::wstring. Also be aware that the size of
wchar_t is different between different compilers. Many Linux based C++ compilers use a different character size and the advice is different again! See https://stackoverflow.com/questions/4588302/why-isnt-wchar-t-widely-used-in-code-for-linux-related-platforms for more info.
Be aware of the scope of variables and when they will be destructed. It is generally bad practice to do
return new object() as the caller might not realize they need to clean up the return value.
Learn about move semantics - they can help a lot with RAII. I'm not aware of an equivalent in C#
Understand what an opaque pointer is. If you're using the Windows API you'll come across a lot of these in the form of
Be wary of storing lists (e.g.
std::deque) of objects which can be inherited as this can lead to slicing where the inherited part of the class is sliced off so only the declared type is left.