@dhayden Thanks for your answer.
I understand that a implicit conversion is done with the number "1" because the copy constructor of A is
A(int a_)
. So the compiler make a conversion on the "1" to an object A to do the addition operation.
Then the compiler search for the operator to match.
However the returned addend order are wrong in your answer @dhayden:
- case 2 matches A c = a + tmp instead of A c = tmp + a:
1 2 3
|
A operator+(A lhs_, A const& rhs_){
return lhs_ += rhs_;
}
|
where:
* (lhs_) attribute m_a: 42
* (rhs_) attribute m_a: 1
Then the function return: lhs_ = lhs_ + rhs.
- case 3 matches A d = tmp -a insted of A c = a - tmp:
1 2 3
|
A operator-(A rhs_) const{ // What does this const mean?
return rhs_ -= *this; //rhs_ = rhs_ - *this -> 1 = 1 - a
}
|
where:
*(rhs_) attribute m_a: 1
*(this) attribute m_a: 42
Then the function return: rhs_ = rhs_ - *this
NOTE: I also don't understand what the cons before the definition body means...
This is actually why I am puzzled because of the order of the addends.
According to my understanding the compiler should find this operator signature:
- case 2:
A& operator +=(A const& rhs_)
- case 3:
A& operator -=(A const& rhs_)
But I am clearly misunderstanding something...