
14 Nov
2008
14 Nov
'08
6:04 p.m.
Hi, did anyone ever suggest a template class which initializes a variable to a certain value? (see example) Is this too trivial? This is useful to prevent uninitialized values in objects. class A { defaulted<int, 1280> _width; defaulted<int, 1024 > _height; defaulted<int, 0> _depth; }; template <typename TYPE, TYPE value> class defaulted { public: defaulted() : _t(value) { } defaulted(const TYPE & value2) : _t(value2) { } operator TYPE & () { return _t; } TYPE * operator & () { return &_t; } const TYPE * operator & () const { return &_t; } operator const TYPE & () const { return _t; } TYPE & operator =(const TYPE & other) { _t = other; return _t; } private: TYPE _t; };