I have a question about function precedence. How does the compiler decide which function to call in quasi ambiguous situations? Is it laid out in the standard or is it implementation dependent?
If you look at the attached code you'll see I'm outputting the value contained by the myint object. Now I can remove the overloaded output operator and the program will appear the run exactly the same because it will use the conversion operator 'operator int()'. So how does the compiler choose? Is it vendor specific or is it decided by the standard? I tried googling but I keep getting operator precedence...So any thoughts on this?
#include <iostream>
class myint
{
public:
myint(int val):itsvalue(val) {}
operator int() { return itsvalue; }
int getitsvalue() const { return itsvalue; }
private:
int itsvalue;
};
//remove overloaded output operator and everything appears the same.
std::ostream& operator <<(std::ostream & out, const myint & m)
{
return out << m.getitsvalue();
}
int main()
{
myint me(47);
std::cout << me << std::endl;
return 0;
}