What's the difference between non-member function and member function when type conversions should apply to all parameters?
That from the book, "Effective C++":
First, make operator*
a member function:
class Rantional {
public:
Rantional(int numerator=0,int denominator=0)
: numerator(numerator), denominator(denominator){
std::cout << "constrctor be called" << std::endl;
}
const Rantional operator*(const Rantional& rhs){
return Rantional(numerator * rhs.numerator, denominator * rhs.denominator); // member function
}
private:
int numerator;
int denominator;
};
Rantional oneHalf(1,2);
Rantional result = 2 * oneHalf; // error
Second, make operator*
a non-member function:
const Rantional operator*(const Rantional& lhs,const Rantional& rhs){
return Rantional(lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator); // non-member function
Rantional oneHalf(1,2);
Rantional result = 2 * oneHalf; // ok
What's different above?
There is a explanation of it in the book, but I can't understand. So can you tell me with a simple description?
The implicit parameter corresponding to the object on which the member function is invoked — the one
this
points to — is never eligible for implicit conversions.