Pages

July 26, 2010

Revew OOP244 - Conversion Operators

The form of a conversion operator declaration is
 operator dataType() const;

example: operator int() const;



In our case, the compiler fails to find an exact, promoted,
or standard-conversion match for any argument-parameter pair,
and searches the single argument constructors for a derived data
type conversion match. The compiler inserts the
constructor code to convert 1234 to a
Student and then calls the assignment
operator that receives a Student as
its right operand. That is,
harry = 1234; // calls operator=(const Student& Student(1234))
If the Student(int) constructor were absent, the compiler could not convert 1234 to a Student and would reject the assignment expression. However, if our class declaration had a Student(long long) constructor, the compiler could convert 1234 to 1234LL, then convert 1234LL to a Student data type, and finally assign the right Student operand to the left Student operand.

To comprehend above please refer more detail

No comments:

Post a Comment