The standard says
A variable is introduced by the declaration of an object. The variable’s name denotes the object.
But what does this definition actually mean?
Does a variable give a name to an object, i.e. are variables just a naming mechanism for otherwise anonymous objects? Or is a variable the name itself?
Or is a variable a named object in the sense that every variable is also an object?
Or is a variable just a "proxy" with a name that "delegates" all operations to the real object?
To confuse things further, many C++ books seem to treat variables and objects as synonyms.
What is your take on this?
About entities, quoting from the C++0x draft:
An entity is a value, object, reference, function […]
Every name that denotes an entity is introduced by a declaration.
A variable is introduced by the declaration of an object
From these statements I draw the conclusion that a variable is a name and thus cannot be an object. This is really confusing the hell out of me 🙂
Variables are named objects. The following create objects that are not variables
The following creates one array variable with name “foo” and 5 unnamed (sub-) objects of type “int”
The following is not a variable in C++03, but has become a variable in C++0x (declared references are variables in C++0x, for details see the link)
Does a variable give a name to an object, i.e. are variables just a naming mechanism for otherwise anonymous objects?
Variables are objects (or references respectively). The entity list (
3/3in C++03) of C++ contains multiple such is-a relationships. For instance, a sub-object is-a object and an array element is-a object and a class-member is-a object or function or type or template or enumerator.The entity list of C++0x looks a bit cleaner to me, and it doesn’t contain “variables”, “instance of a function” (what that kind of entity even is has never been apparent to me), “sub-object” and “array element” anymore. Instead it added “template specialization” which either are functions, classes or templates (partial specializations).
The C++ object model at
1.8saysSo if you like, you can formulate the statement as “The object’s name denotes the object.”.