Almost all c++ projects have classes with copy c-tor/copy operator/serialize method etc. Which usualy doing something with all members.
But sometimes developers forg
template
class SafeMember {
public:
T _; /* short name for convenience */
SafeMember(T const& obj) : _(obj) { }
};
Used like this:
class Student {
public:
Student(string surname, Color hairColor)
: surname(surname)
, hairColor(hairColor) { }
Student(Student const& other)
: surname(other.surname)
, hairColor(other.hairColor) { }
Student& operator=(Student const& other) {
surname = other.surname;
hairColor = other.hairColor;
return *this;
}
string getSurname() const { return surname._; }
// The foo._ syntax is better than implicit conversion because
// it lets us call member functions, like substr in this example:
bool isSlavic() const {return surname._.substr(surname._.size()-2)=="ev";}
void dyeHair(Color newColor) { hairColor = newColor; }
private:
SafeMember surname;
SafeMember hairColor;
};
Now when you add a "SafeMember
" member and forget to update your copy-constructor, the compilation will helpfully fail.
And for a "no-op" hint, the developer would add an initializer like ":age(0)".
Note: this doesn't protect your operator=() or serialize() functions from bit-rot, only the constructors. Hopefully, though, this should be enough: once you see your omission from the constructors, you will probably remember to go through the other functions as well.