0

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;
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by gerard4143
0

Short answer: It is in the standard.
Medium answer: It is both in the standard AND a source of much confusion, because of the complexity of the issue.

0

Short answer: It is in the standard.
Medium answer: It is both in the standard AND a source of much confusion, because of the complexity of the issue.

Thanks for the prompt rely to one of my many C++ questions. Its good to know the standard will ensure consistent usage in this case..Again thank-you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.