Skip to main content
gamedev.net
🔒 Locked

Expression is a value

Started by Axiverse May 6, 2004 at 6:42 PM 8 replies 1.7k views
Original Post
Axiverse
Axiverse
has anyone ever gotten this message before:? Expression is a value and therefore cannot be the target of an assignment This happens when i try to modify a structure passed through a property... is there anyway to pass a reference of an object through the get of a property?
Hybrid666
Hybrid666
this sounds like a result, in v simple terms, of sumthing like:

5 = array.size();

or something similar, u just cant do it, 5 is a value.

beyond that i cant really help

Hybrid
C-Junkie
C-Junkie
what compiler is that?
Chris Hare
Chris Hare
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.
Hybrid666
Hybrid666
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
Chris Hare
Chris Hare
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

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.
Chris Hare
Chris Hare
PS: f(7) = 8 doesn''t make sense in C++, Prolog, for instance, uses this type of expression in matching.



blah blah blah.
Fruny
Fruny
quote:
Original post by Chris Hare
PS: f(7) = 8 doesn''t make sense in C++, Prolog, for instance, uses this type of expression in matching.



Were f to return a non-const reference, it would be perfectly legitimate, and could very well "make sense".
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Chris Hare
Chris Hare
Correct, but as defined in my post, it doesn''t.


blah blah blah.
Jingo
Jingo
quote:
Original post by Chris Hare
The subject of an assignment must be an l-value (a variable, array element etc).



You can assign to rvalues of class type.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.