<<< The dual operator[] idiom | Index | Conversion by constructor mix-up >>> |
Defining a constructor which takes one argument of type T gives conversion from T to your type:
// rational.h class Rational { private: int m_n; int m_d; public: Rational( int num ) // convert int to Rational : m_n(num), m_d(1) { } }; void print_rational( Rational const& rat ); // main.cpp #include "rational.h" int main( ) { Rational rat = 3; // Ok: same as r(3) print_rational(3); // Ok: one-level implicit conversion return 0; }
<<< The dual operator[] idiom | Index | Conversion by constructor mix-up >>> |