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.
|
migrated from stackoverflow.com Feb 18 '11 at 0:55
PS: Since I talked about new, delete, raw memory etc, then let me add an interesting difference between C++ and C# : In C++, |
|||||||||||||||||||||
|
|
The most fundamental one is of course related to the lack of a garbage collector:
Other than that, I'd say there are some stylistic issues you should get used to. C# is still, despite its flirtation with functional programming, an OOP language. C++ is a multi-paradigm language, and in modern C++, OOP just doesn't play a very big role. You should definitely look into generic programming, though. Understand how to use the STL (the data structures, iterators and algorithms in the standard library). And feel just a bit dirty when you write a virtual function. |
|||||||||||||||||||||
|
|
Automatic memory management and garbage collection is probably something you could be missing in C++. But IMHO what you will miss most is the consistency of the .NET framework where you have been taking lots of things for granted simply because they are built into the BCL. |
|||||||||||||||
|
|
The biggest problem I've seen is the tendency to new objects all over the place and use them on the heap. Sure, they may take a lot of care deleting them as there's no GC, but the problem is allocating on the heap in the first place. C++ works better putting stuff on the stack and copying it about. If you do still need a single shared object, use shared_ptr to contain it. Mind you, this isn't exclusive to C# devs, I've seen a a few web devs do the same kind of thing. |
|||||||
|
|
Garbage collection is a big one, but if you use smart pointers you can avoid most of the trouble with that. A more pervasive issue is that of exception safety. This isn't so much a C# habit that will get you in trouble, but a lack of one. In C#, you get safety with |
|||||||||||
|
|
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 Be aware of the scope of variables and when they will be destructed. It is generally bad practice to do 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 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. |
|||||
|
|
Forget about try-(catch-)finally and get used to do resource allocation/deallocation via constructors/destructors (RAII). |
|||
|
|
|
Mostly about garbage collection. You have to release every dynamically allocated memory. One more thing is that pointers can be invalid. In C# reference can point only to object or to Array bounds check. No one grantees that you don't overcome the boundary. In C++ you can pass object by value, not only by reference. It may cause useless copying. |
|||||||||||||||
|
|
Avoid programming in C++ as if it was C#, that's the practice you should avoid. Actually learn the language, it's quirks, the good parts and it's idioms. |
|||
|
|
|
The most obvious would probably be taking garbage collection for granted. Un-referenced objects have to be manually disposed in C++, but in C# you have the GC to take care of them. |
|||
|
|
|
Memory management, passing conventions and pointers, although if you use a library like Boost it's probably less of a problem. However, if you're teaching yourself C++ it's probably a good idea to not use any libraries and learn the basics first. |
|||
|
|
|
Memory management, watching out for memory leaks and getting used to not having automatic garbage collection. |
|||
|
|
