Are there any lint-like guidelines for C++?
Yes, there are some practices which are generally considered dangerous. However none of these are universally “bad,” since situations arise when even the worst of these is needed: • A class Fred’s assignment operator should return *this as a Fred& (allows chaining of assignments) • A class with any virtual functions ought to have a virtual destructor • A class with any of {destructor, assignment operator, copy constructor} generally needs all 3 • A class Fred’s copy constructor and assignment operator should have const in the parameter: respectively Fred::Fred(const Fred&) and Fred& Fred::operator= (const Fred&) • When initializing an object’s member objects in the constructor, always use initialization lists rather than assignment. The performance difference for user-defined classes can be substantial (3x!) • Assignment operators should make sure that self assignment does nothing, otherwise you may have a disaster. In some cases, this may require you to add an explicit test to your as
Yes, there are some practices which are generally considered dangerous. However none of these are universally “bad,” since situations arise when even the worst of these is needed: • A class Fred’s assignment operator should return *this as a Fred& (allows chaining of assignments) • A class with any virtual functions ought to have a virtual destructor • A class with any of {destructor, assignment operator, copy constructor} generally needs all 3 • A class Fred’s copy constructor and assignment operator should have const in the parameter: respectively Fred::Fred(const Fred&) and Fred& Fred::operator= (const Fred&) • When initializing an object’s member objects in the constructor, always use initialization lists rather than assignment. The performance difference for user-defined classes can be substantial (3x!) • Assignment operators should make sure that self assignment does nothing, otherwise you may have a disaster. In some cases, this may require you to add an explicit test to your as