What is the difference between values and objects?

Viewed 1018

I was reading about value types and values and objects were mentioned as if they meant something different, so I assume they do. I then tried to do some research but without luck. I know that a value is an expression that cannot be evaluated further, but isn't an object just the "container" for a value? Also, based on my terminological understanding of expressions, would it be weird to say that an object holds an expression(since a value is an expression)?

I will not start making too many guesses because that could will probably just complicate it all and might redirect the focus away from the difference between values and objects.

Edit: To the people who have marked this as "too-broad", isn't the following question just as broad if not broader What are rvalues, lvalues, xvalues, glvalues, and prvalues? Also, there are so many other "difference between" questions out there on SO, and many of them are as broad as the one I linked.. So why is this particular question not valid?

2 Answers

This term means slightly different things in different parts of computer science (as you can see from the discussion on its wiki page). I'm framing this answer in the context of C++ and "value types" mentioned in the question - the authoritative references here would be Alexander Stepanov's "Elements of Programming" and the lectures on value-semantic types by John Lakos.

A value is a member of a set that exists independently of its representation.

The member of the set of integers that can be repesented as "fifteen", "15", "0xf", "十五", or "ⅩⅤ" is the same, universal, value. I can write it down on two pieces of paper, add one to both, and both pieces of paper will have a representation of the same exact value 16, even if I use different notations (this is what it means to have "value semantics")

An object is a region of computer memory that consists of bits (binary digits), and has a type. One of the things the type does is determine the mapping between the state of those bits and the value we treat the object as.

An object of type int8_t consisting of the bits 00001111 is, on existing computers, considered to represent the integer value "fifteen".

Related