Here's a simple question.
I've done plenty of work using both C & C# (2.0) but never anything in C++. What can I expect to be different when learning C++? Will there be any big gotcha's or hurdles I should pay attention too? Does anyone good crash course book/website recommendations for learning C++ for the experienced programmer?
The main difference I can think of is that C++ is far more of a multi-paradigm language than C and C#. In C#, OOP is still the paradigm. It's a OOP language before anything else, and if you're not doing OOP, the C# community will tell you you're doing it wrong. (although C# has added quite good support for a few bits of functional programming as well, over the last few years).
In C++, OOP is, well, it's supported and you can use it when you feel like it, but all the fuss is around generic programming. C++ templates allow a wide range of clever, reusable and generic libraries, and achieve many of the same goals as old-fashioned OOP, but without the big inheritance hierarchies and with virtually no coupling between classes. The standard library contains many examples of this
In C++, a lot of C constructs, while still legal, are basically shunned:
boost::shared_ptr
or std::auto_ptr
, or with referencesOf course, there are exceptions to every one of these points, but as a general rule of thumb, C++ code will, unlike C code, pretty much eliminate all use of these.
And more than in C#, classes are really workhorses doing a lot of the heavy lifting. In C#, a class is little more than a bit of scaffolding, a container to stick all your methods in. Sure, it has a constructor, and it may implement Dispose(); but C++ takes it a lot further, and you have:
The destructor is probably the most important concept in C++. It is vital to RAII, which is how memory or other resources are managed, because it is automatically called when an object goes out of scope. That allows your classes to make a whole lot of guarantees that are impossible to achieve in C or C#. For example, boost::thread provides a scoped locks which is guaranteed to be released when it goes out of scope, whether the function returns normally, an exception is thrown, or anything else. So when using this library, the user doesn't have to worry about releasing locks or other resources. It just happens automatically, as soon as you're done with them.
In a sense, this gives you a lot more hooks to customize the behavior of your class. Unlike in C#, you control exactly what happens when a simple assignment is executed. You control what happens when the class goes out of scope, when it is initialized from scratch or as a copy of another object. This allows a well-written class to be almost impossible to use incorrectly. (Almost)
Apart from this, templates and template metaprogramming are concepts you'll probably run into. They're extremely powerful tools, so make sure you're on friendly terms with them. :)
Here are some from my point of view:
HttpRequest
class built-in in C++.Interface
as in C#. You
will use abstract class (and
multiple inheritance) instead.You can also learn C++/CLI to mix your .NET code and native C++ together to get the best of both world.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With