[ fromfile: conversions.xml id: conversions ]
Example 2.19. src/ctor/conversion/fraction.cpp
class Fraction { public: Fraction(int n) : m_Numer(n), m_Denom(1) {} Fraction(int n, int d ) : m_Numer(n), m_Denom(d) {} Fraction times(const Fraction& other) { return Fraction(m_Numer * other.m_Numer, m_Denom * other.m_Denom); } private: int m_Numer, m_Denom; }; int main() { int i; Fraction frac(8); Fraction frac2 = 5; frac = 9; frac = (Fraction) 7; frac = Fraction(6); frac = static_cast<Fraction>(6); frac = frac2.times(19); return 0; }
Single argument ctor defines a conversion from int. | |
Conversion constructor call. | |
Copy init (calls conversion ctor too). | |
Conversion followed by assignment. | |
C-style typecast (deprecated). | |
Explicit temporary, also a C++ typecast. | |
Preferred ANSI style typecast. | |
Implicit call to the conversion constructor. |
The matching constructor is the one-parameter version.
Effectively, it converts the integer 8
to the fraction 8/1
.
The prototype for a conversion constructor typically looks like this:
ClassA::ClassA(const ClassB& bobj);
For example, if frac
is a properly initialized Fraction
as defined in Example 2.19, you can write the statement
frac = frac.times(19);
So, that statement calls three Fraction
member functions:
Fraction::operator=()
to perform the assignment.
Fraction::times()
to perform the multiplication.
Fraction::Fraction(19)
to convert 19
from int
to Fraction
.
The temporary Fraction
object returned by the times()
function exists just long enough to complete the assignment and is then automatically destroyed.
You can simplify the class definition for Fraction
by eliminating the one-parameter constructor and providing a default value for the second parameter of the two-parameter constructor.
Example 2.20. src/ctor/conversion/fraction.h
class Fraction { public: Fraction(int n, int d = 1) : m_Numer(n), m_Denom(d) {} Fraction times(const Fraction& other) { return Fraction(m_Numer* other.m_Numer, m_Denom* other.m_Denom); } private: int m_Numer, m_Denom; };
The keyword explicit
prevents the compiler from automatically using that constructor for implicit conversions. [19]
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |