views:

326

answers:

7

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 :)

+2  A: 

Variables are names that you give to objects, so yes, objects are, by and large, anonymous.

Marcelo Cantos
+16  A: 

Variables are named objects. The following create objects that are not variables

new int // create one int object
std::string() // create one string object

The following creates one array variable with name "foo" and 5 unnamed (sub-) objects of type "int"

int foo[5];

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)

extern int &r;

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/3 in 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.8 says

An object can have a name (clause 3).

So if you like, you can formulate the statement as "The object's name denotes the object.".

Johannes Schaub - litb
+1 My take exactly
Andreas Brinck
FredOverflow
An more illustrative example of an object that isn't a variable is an anonymous object passed in as an argument to something else.
detly
@FredOverflow good point, i'll incorporate it into the answer after reading some FCD :)
Johannes Schaub - litb
Thanks for mentioning the entity list, I have updated my question. Please read the update and reply ;)
FredOverflow
@FredOverflow i don't come to the same conclusion. The text says that a name that denotes an object is introduced by a declaration. And it says that the declaration of an object introduces a variable. I can't conclude from that that a variable is a name.
Johannes Schaub - litb
Could you post the complete entity list of C++03? If that list clearly states that variables are objects, the case is solved.
FredOverflow
@FredOverflow the entity list does not state such relations directly. It just lists them.
Johannes Schaub - litb
Is there any other sentence that starts with "a variable is"?
FredOverflow
@FredOverflow: Is that a fallout of [our recent discussion of what's an object in C++](http://stackoverflow.com/questions/2910587/2910753#2910753)?
sbi
@sbi No, but thanks for reminding me to answer in that discussion :)
FredOverflow
A: 

I think that this definition is quite clear.

The variable is introduced by declaration and denotes the object. Who introduces the variable? You do of course, and thus it is you who uses it.

A variable is really only a convenience for you the developer. It is a fundamental aspect of most programming languages not just C++. A variable mearly gives a symbolic name to a useable entity that occupies storage, such that it can be referenced and used at a future point in your source code.

For example if you declare a variable in a method as such:

int x = 5;

This will be reduced by the compiler to some offset from the stack pointer, say SP + 0x003.

At some point later you can say:

x = 52;

In this case the area of stack memory SP + 0x003 will contain the bytes that describe the number 52.

You declare the variable to be of a certain type so that the compiler can work out how much space the data occupies in memory. Without variables, you would have to manage all of the arrangement of information yourself and you would probably be coding in assembly or lower.

Adrian Regan
I find your definition "The variable is introduced by declaration and denotes the object" very clear, but unfortunately, that's not what the standard says :) According to the standard, one declares *objects*, and it is not the variable that denotes the object, but the variable's *name* :(
FredOverflow
Hey, maybe I should even become a standards writer :-). As an aside, I think learning a language by reading it's standards document is a form of self harm...
Adrian Regan
A: 

A variable is really a name given to an object in memory and hence an object is an anonymous type in that respect just at the point before compilation, when the compilation occurs, the variable is kept track of during the syntactical and parsing phase, then when the linker kicks in, that variable will have a memory address assigned to it, although at run-time, that memory address will be correctly off-setted somewhere to take into account of dynamic linking or static linking. Hence the variable can then be easily referenced aka the memory address that the variable is assigned to.

Really, in a nutshell, the variable is to help the programmer to work out the junction points of execution where that variable is referenced in terms of machine code.

tommieb75
+1  A: 

What is your take on this?

Variable is a block of memory on stack or in code segment, or a value in a register (if the size of variable is small enough, although normally it is still value in a memory while registers hold temporary results), with name provided for programmer's convenience. Name of variable does not exist after compilation (we're not talking about macro tricks here). Any access to the variable is resolved into memory access, so technically variable is an address of corresponding data block, and that address isn't stored anywhere. Think about declaration of variables in assembly languages - variable "kinda" exists, but it is still merely an offset to the data block.

SigTerm
Objects exist on the heap, on the stack, or in the data segment.
ChrisW
You understood me, so there is no reason for nitpicking.
SigTerm
The only good answer really. Variables are really just pointers to memory addresses which the compiler figures out for you because it's more idiomatic to use a name rather than a number.
Lajla
+2  A: 

Variable is a name for the object. You access the object through this named entity.

fastcodejava
+1  A: 

A variable is simply an entity with a type and a name

Chris