TITLE: insisting on initialized data member declarations [ I am not advocating this as necessarily a good design practice. I put it here only as a "trick" to be aware of. -adc ] PROBLEM: Artyom@kansmen.com (Art Shelest) Anyone has a handy technique for initializing class members in the constructor? For example: class A { public: A(); private: int m_int; int m_long; }; Currently I use A::A() { m_int = 0; m_int = 0; } and have to remember to update constructor every time I add a new member to the class. I would like an easier alternative. RESPONSE: jcoffin@rmii.com (Jerry Coffin), 11 Feb 96 template class initialized { T value; public: initialized() : value(0) {} operator T() { return value; } initialized &operator=(const T &other) { if (this != &other ) value=other; return *this; } }; class A { initialized my_int; initialized my_long; public: A() {} void do_something() { my_int = 10; my_long = 20; } }; This is probably most useful as a container for primitive types -- when your define a class, if it makes sense to provide default values, that's best done in the default ctor. E.g: class my_thing { sometype value; public: my_thing(sometype initial_value = 0) : value(initial_value) {} }; This allows tailoring the value(s) and type(s) used to those appropriate to the class being initialized.