🔒 Locked
Expression is a value
Advertisement
Original Post
I imagine something like house.getAddress() = "12 Bakers Ave"; has been written. You''ll need to take the value to be assigned as a parameter in a new function (e.g. house.setAddress(const char*))
You shouldn''t return the inner variable as a reference, as this violates encapsulism, though it is possible.
blah blah blah.
You shouldn''t return the inner variable as a reference, as this violates encapsulism, though it is possible.
blah blah blah.
wouldn''t that run fine though?
house.getAddress() = "12 Bakers Ave";
surely returns a copy (assuming a reference isn''t returned) and therefore assigns the copy the value "12 Bakers Ave" ?
im pretty sure that wouldnt throw up an error of any kind under VC++ even though it is probably not what the programmer wished to do.. it would just happily assign the value "12 Bakers Ave" to the copy of the house class? i maybe wrong of course
Hybrid
house.getAddress() = "12 Bakers Ave";
surely returns a copy (assuming a reference isn''t returned) and therefore assigns the copy the value "12 Bakers Ave" ?
im pretty sure that wouldnt throw up an error of any kind under VC++ even though it is probably not what the programmer wished to do.. it would just happily assign the value "12 Bakers Ave" to the copy of the house class? i maybe wrong of course
Hybrid
The subject of an assignment must be an l-value (a variable, array element etc). The result of, say, house.getAdddress() is not an l-value, the result of
is not an l-value. Therefore, this cannot be done
mainly, because it doesn''t make sence, but also because the returned result is an anonymous value (r-value anyone?).
As stated earlier, you can pass a reference to an inner variable whose scope is sure to be greater than its reference, but it is not a good idea.
blah blah blah.
int f(int a)
is not an l-value. Therefore, this cannot be done
f(7) = 8;
mainly, because it doesn''t make sence, but also because the returned result is an anonymous value (r-value anyone?).
As stated earlier, you can pass a reference to an inner variable whose scope is sure to be greater than its reference, but it is not a good idea.
blah blah blah.
PS: f(7) = 8 doesn''t make sense in C++, Prolog, for instance, uses this type of expression in matching.
blah blah blah.
blah blah blah.
Correct, but as defined in my post, it doesn''t.
blah blah blah.
blah blah blah.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement