Group: comp.lang.c++
From: Niels Dekker - no return address
Date: Saturday, February 16, 2008 4:09 PM
Subject: Re: Cass object returning a value by name?

Jerry Coffin wrote:
> operator int() { return XNumber; }

Basically Jerry is right, but you'd probably want to indicate that the
conversion does not modify your X object, by adding a "const":

class X {
int XNumber;
public:
operator int () const { return XNumber; }
};

Lilith wrote:
> Many thanks. I anticipated the hazard and have tried think of an
> instance where this might be a problem but at this time I can't.
> However, I'll be on the alert as I progress through this development
> and if it presents a problem I'll go the less intuitive route.

The dangerous thing about those conversion operators is that their
conversion happens so "implicitly". Fortunately the next C++ Standard
will allow you to declare the conversion operator as "explicit". But I
guess your compiler won't support it yet. Anyway, if you want to know
more, read: "Explicit Conversion Operator Draft Working Paper (revision
3)", by Lois Goldthwaite, Michael Wong, Jens Maurer, and Alisdair
Meredith - www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2437.pdf

[Oops, my original reply went to the wrong newsgroup: comp.lang.c It's
because of a bug in my good old (VERY old) newsgroup client. Hmmm,
maybe I should upgrade!]

Kind regards,
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

Safety Articles | Usenet Groups | 20lbs in 30 days