Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I 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!"



This 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_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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: