#### Archived

This topic is now archived and is closed to further replies.

# Expression is a value

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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

##### Share on other sites
what compiler is that?

##### Share on other sites
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.

##### Share on other sites
wouldn''t that run fine though?

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

##### Share on other sites
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.

##### Share on other sites
PS: f(7) = 8 doesn''t make sense in C++, Prolog, for instance, uses this type of expression in matching.

blah blah blah.

##### Share on other sites
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".

##### Share on other sites
Correct, but as defined in my post, it doesn''t.

blah blah blah.

##### Share on other sites
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.

• 10
• 18
• 14
• 18
• 15