I was recently at a talk where the speaker was discussing the history of C++. He argued that one problem with C++ was that its design requirements included backward compatibility with C code, and one fallout of this was the requirement to support all previous types of error handling as well as adding exceptions. That is, C++ supports:
It is true that C++ supports all 3 of these mechanisms, as well as some other cruft left behind from C that unnecessarily encumbers the language. The speaker's point on this one particular case, however, is lost to the fact that every language supports the first two.
There has been much discussion over the years about the usefulness of operator overloading in C++ as well as the ability to get it right.
In reality, it's hard to get it wrong as long as you follow the canonical forms of the operators and don't do unexpected things like overloade the + operator to perform a subtraction operation. Also, the types that your overloaded operators work with should be consistent.
In case you find yourself asking where I get the stuff from that I blog about and wondering if I'm just making stuff up; many of the "Nobody Understands C++" articles on this website are inspired by principles found in the book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu. The book is a collection of items from previous books that the pair has worked on together. As such, if you do not own any of the previous books this one makes a great addition to your library.
The book covers 101 best practices guidelines for your C++ code. It does not cover things like indentation level and style of comments, the things that we have all come to consider to be part and parcel with a standards document. In fact, it explicitly avoids talking about those things and says that they should be internally consistent within each file. "C++ Coding Standards" covers topics on program design, object lifetime management, namespaces, generic programming (templates) and the standard library.
It is best to avoid using pointers in C++ as much as possible. The use of pointers can lead to confusion of ownership which can directly or indirectly lead to memory leaks. Even if object ownership is well managed simple (and difficult to find) bugs can also lead to memory leaks.
Security Risks
Also, by avoiding the use of pointers we can avoid and sometimes eliminate common security holes such as buffer overruns. See the last example for more information about this.
Examples
Common instances were pointer usage can be avoided:
We've now completed 5 articles in the "Nobody Understands C++" series so here we are going to recap the misconceptions we have covered.
On occasion you will read or hear someone talking about C++ templates causing code bloat. I was thinking about it the other day and thought to myself, "self, if the code does exactly the same thing then the compiled code cannot really be any bigger, can it?"
Here are the test cases presented, first without the use of templates, second with the use of templates. Exactly the same functionality and exactly the same code output:
Recent comments
5 hours 54 min ago
1 day 12 hours ago
2 days 18 hours ago
3 days 3 hours ago
1 week 5 days ago
2 weeks 2 days ago
2 weeks 4 days ago
3 weeks 1 day ago
3 weeks 1 day ago
3 weeks 3 days ago