At some point, you find yourself writing a C++ method that ends up having a lot of repetitious code that is very context-specific to that method. Sure, you could create a private method that encapulates the repetitious code. But, that approach brings about 2 problems that I respectively call class namespace pollution (declarations at class [...]
Archive for the ‘Programming Languages’ Category
C++ PowerTip #4: Use Functors for Local Functions
Posted: September 15, 2010 in C++, PowerTips, Programming LanguagesTags: c++, functors, overloads, powertips
C++ PowerTip #3: Safe Complex Object Construction
Posted: September 10, 2010 in C++, PowerTipsTags: c++, constructor, destructor, exceptions, geekiness
Writing long constructors that perform a number of operations outside of merely initializing data members is generally discouraged due to the inherant problems with error handling. However, there are times when an object must undergo complex initialization before it can be used. Generally, I prefer to make such initialization as transparent as possible to simplify [...]
C++ PowerTip #2: Prevent an Object from Being Deleted
Posted: September 10, 2010 in C++, PowerTipsTags: classes, destructors, operator delete, operator new, overloading, templates
There are cases where you must provide a pointer to an instance of a C++ class but you need to prevent the object from being destroyed via operator delete. The preferred approach is to make the class destructor either protected or private. Doing this will cause the compiler to generate an error when calling delete [...]
C++ PowerTip #1: Initialize Data Structure to Zeros
Posted: May 12, 2010 in C++, Programming LanguagesTags: c++, powertips
To quickly initialize a data structure with all zeros (i.e. zero’ed memory), do this: SOME_STRUCT myStruct = { NULL };