C++ difference between variable and object

Started by
10 comments, last by King Mir 11 years, 1 month ago

A memory is a set of words, each with an address and a content. The addresses are values of a fixed size, called the address length. The contents are values of another fixed size, called the word length. The content of an address is obtained by a load operation. The association of a content with an address is changed by a store operation. Examples of memories are bytes in main memory and blocks on a disk drive.

An object is a representation of a concrete entity as a value in memory. An object has a state that is a value of some value type. The state of an object is changeable. Given an object corresponding to a concrete entity, its state corresponds to a snapshot of that entity. An object owns a set of resources, such as memory words or records in a file, to hold its state.

See:

http://www.drdobbs.com/this-weeks-developer-reading-list/218600582

http://cpp-next.com/archive/2009/11/%E2%80%9Celements-of-programming%E2%80%9D-chapter-1-foundations/

Also, this time from "C++ Primer":

Terminology: What is an Object?


C++ programmers tend to be cavalier in their use of the term object. Most
generally, an object is a region of memory that can contain data and has a
type.


Some use the term object only to refer to variables or values of class types.
Others distinguish between named and unnamed objects, using the term

variable to refer to named objects. Still others distinguish between objects
and values, using the term object for data that can be changed by the
program and the term value for data that are read-only.


In this book, we’ll follow the more general usage that an object is a region
of memory that has a type. We will freely use the term object regardless of
whether the object has built-in or class type, is named or unnamed, or can
be read or written.

Advertisement
There is a problem with defining an object as something in memory -- it implies objects cannot be in registers. Now you can program in C++ without knowledge of registers, but eventually one's likely to run into the term. A register is like a memory location, except instead of being represented by a pointer, it is represented by an identifier in assembly language. There are few registers, but using them is faster than using memory. C++ uses registers implicitly, so it's not something that's important to remember when learning to program.

Defining an object as something that has an address is even worse, because you cannot take an address of an rvalue/temporary, but rvalues are objects. An rvalue is a value returned by a function, as a result of a cast, constructor call, or similar operation. For primitive types, and in all of C, an rvalue can only be found on the right side of an assignment operation, thus the name.

So with these two points in mind it's better to say that an object "has storage."

This topic is closed to new replies.

Advertisement