44

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.

  • 1
    wouldn't it be more helpful to ask what you should do instead, rather than simply "what are the errors I'll make"? Most of the answers you're getting are just telling you why you'll hate C++. – jalf Feb 15 '11 at 19:04
  • 2
    I'm not asking "what errors will I make". I'm asking of the habits that I'v learned using C#, which ones will cause me problems in C++. – Donny V. Feb 15 '11 at 19:12
  • 3
    Using C# in the first place (just kidding). ;-) – Thomas Matthews Feb 15 '11 at 20:24
  • 2
    @James: I disagree. I don't see a problem with subjective questions, as long as it is reasonably possible to distinguish between a "good" and a "bad" answer. (Practically every question is subjective on some level anyway). I'll edit the question text a bit and vote to reopen. – jalf Feb 16 '11 at 2:41
  • 3
    This question is like asking "what common practices speaking Spanish can lead to difficulties in Italian". Languages have a common ground, but if you are trying to adapt Spanish to speak Italian you would always have a weird Spanish accent and sometimes even build sentences in a completely awkward way for Italian. – Trinidad Feb 17 '11 at 13:10
8

RAII is definitely one of the key aspects of C++ to learn. Other posters have described it, but I will add that it is quite similar to C#'s IDisposable interface combined with the using keyword.

Be wary of char and 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 char and 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.

Avoid the use of naked pointers, instead use unique_ptr or shared_ptr + Use make_shared<>() to create a shared_ptr. Unfortunately there is no unique_ptr equivalent at the moment.

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 HANDLEs.

Be wary of storing lists (e.g. std::vector or 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.

  • ISO/IEC 14882:2011 § 3.9.1 Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. From this quote we can understand that the char encoding is not ASCII but the implementation basic character set. It just happened to be ASCII on some (most) platforms. – authchir Jan 5 '13 at 2:36
  • Thanks for the comment. I've tried to add enough relevant information in without going over the top. I hate that strings are so complicated in C++! – Steve Jan 5 '13 at 13:10
  • Better use std::string and UTF-8. Now even Windows 10 finally allowed it for the narrow character set. – Deduplicator Apr 5 at 16:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.