This page serves as documentation of C++ tips, hints, and gotchas we found during use of the language (and the Standard Library, et al).
WD Style and Design Considerations
Wilcox Development Solutions C++ Style Guidelines
C++ Compile Speed Tips & Design Considerations
Containers and Iterators
Standard Containers and Objects
Another guide to writing a custom iterator
Examples of "standard" iterator usage
I always forget how to construct a for loop with iterators, but now here is an example snippet (by Tom Becker)
1
2 for (vector<T>::iterator iter = myVectorVariable.begin(); iter != myVectorVariable.end(); ++iter)
3 {
4 ; //do stuff here. depending on what kind of std::container you are iterating over, iter could be different things
5 }
Misc
Variable Argument Lists (in C)
Singleton Pattern
Bitwise operation example
I always get these confused
1
2 long avalue = 0xFFFFEEEE;
3 printf("%x", avalue & 0x0000FFFF );
4 --> EEEE
C++ Style Casts
An article on C++ cast operations in the ACM
However, above article has the following issues
- static_cast casts void* too (as long as void* points to a former object and you know/guess the type correctly) (Stroustrup Special Edition 15.4.2.1)
- const_casts are only guaranteed if the variable was non-const to begin with: (Stroustrup Special Ed 15.4.2.1 / 10.2.7.1)
- dynamic_cast does NOT cast void* to a (pointer) object (Stroustrup Special Ed 15.4.2.1). I got a nice GCC compile error when I tried). The article does get it right that you can cast a (pointer) object to void* to tell if the type is polymorphic or not. (Stroustrup 15.4.1)
- dynamic_cast does NOT throw a Bad_cast exception if you pass it a reference - it throws a bad_cast exception.
- should have explicitly stated that reinterpret_cast is only guaranteed to work if the result type is the type used to define the value the value involved AND that the result has the exact same bit pattern as its argument. (Stroustrup 6.2.7)
(See Stroustrup's reasoning on why C++ casts exist)