What are some C++ related idioms, misconceptions, and gotchas that you\'ve learnt from experience?
An example:
class A
{
public:
char s[1024];
cha
One seldom used, but handy C++ idiom is the use of the ?: operator during the constructor chain.
class Sample
{
const char * ptr;
const bool freeable;
Sample(const char * optional):
ptr( optional ? optional : new char [32]),
freeable( optional ? false : true ) {}
~Sample( ) { if (freeable) delete[] ptr; }
}
C++ doesn't allow const values to be changed inside the body of the constructor, so this avoids const-casts.