3 ms·
This feels like a C++ tutorial from mid-90s. C++ code written today looks nothing like what's presented in this article. Besides, some poor sample code choices
by obiterdictum 15y ago
This feels like a C++ tutorial from mid-90s. C++ code written today looks nothing like what's presented in this article. Besides, some poor sample code choices jump out at me, like throwing an int and using C library. I realize they are there to illustrate a point, but this reinforces bad habits.
In fact, if you are learning C++, you better start it as a new language, rather than a tumor grown on the side of C. These languages are too idiomatically different.
- hugh3 15y agoI learned C++ in the mid-90s and haven't changed my style since. What am I doing wrong? My strategy for writing in C++ is to write in C until I suddenly think "Hey, I should totally use an object here!"
- zohebv 15y agoThis is my personal style. I don't really use the OOP much. I get as much work done as possible via STL. Use RAII for resource cleanup. Completely avoid new/malloc/throw etc. If forced to use pointers use smart_ptr (possible use case - polymorphism) and don't forget about enable_shared_from_this. http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_s... My C++ code is typically 3X shorter than corresponding C with fewer bugs and faster performance. The main issue is avoiding memory corruptions and getting away from pointers and memory leaks by leveraging the STL heavily. I do use objects and make sure the members are C++ types rather than some C-style pointers, so that the copy and equality semantics are well defined. The key thing for me is avoiding pointers at all costs as pointers complicate memory management immensely. Use STL vector, set etc and in the worst case smart_ptr.