Sujet : Re: how cast works?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 08. Aug 2024, 23:40:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v93hgg$9q8p$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
Thiago Adams <
thiago.adams@gmail.com> writes:
...
I also curious about how bool works.
>
Values converted to bool became 0 or 1.
When this conversion happens, at read or write? Both?
You can take a value obtained by reading an object, or a value produced
by evaluating an expression, and convert that value to a different type.
That value can later be stored in an object, or it could be used as one
of the operands for an expression. The conversion isn't associated with
either the read or the write. Many conversions occur implicitly, a cast
is used to explicitly make a conversion occur.
int x = 3;
bool b = x;
In the above code, an implicit conversion from int to bool occurs after
reading the value of 3 from x, and occurs before writing to bool.
b = !(bool)(x-3);
In this code, the conversion occurs after the value of 3 is retrieved
from x, and after 3 is subtracted from it. That result of 0 is then
converted to bool, and then the ! operator is applied to it. Finally,
the result is written to b. So you see, it doesn't make sense to connect
the conversion with either the read or the write.