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;
}

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.