// // Constructors // // In object-oriented languages constructors have the role to initialize // newly created objects. Different styles are used including factory // methods. Structures/classes without constructors can be instantiated // without constructor call. If at least one constructor is defined for // a class, objects must be created through one of the constructors // // // ... but also for conversions // class date { public: // constructor date( int y=2000, int m=1, int d=1); // explicite constructor explicit date( const char *s); // ... private: // ... int year; int month; int day; }; // // secondary question: member or global operators? // bool operator<( date d1, date d2); inline bool operator==( date d1, date d2) { return !(d1<d2 || d2<d1); } inline bool operator!=( date d1, date d2) { return d1<d2 || d2<d1; } inline bool operator<=( date d1, date d2) { return !(d2<d1); } inline bool operator>=( date d1, date d2) { return !(d1<d2); } inline bool operator>( date d1, date d2) { return d2<d1; } int main() { date d(2001, 3, 12); if ( d < 1999 ) // works { // ... } else if ( d < "2000.1.12") // does not work: explicit constructor { // ... } else if ( d < date("2000.1.12") ) // works: explicit call of constructor { // ... } }