简体   繁体   中英

Are variables considered objects in c++?

There seems to be disagreement in the c++ community to whether or not variables are objects.

The cpprefrence says that variables are named objects whereas the standard says variable names denotes a reference or an object. I'm wondering if someone can explain it clearly.

There is no controversy.

What does the standard say?

The C++ standard says that C++ works with objects:

[intro.object]/1 : The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition, by a new-expression, when implicitly changing the active member of a union, or when a temporary object is created. An object occupies a region of storage in its period of construction, throughout its lifetime, and in its period of destruction.

The object model and the type definition makes clear that it's always about objects, even for the simplest types:

[basic.fundamental]/1 : Objects declared as characters (char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character.

The standard also clarifies what a variable is:

[basic]/6 : A variable is introduced by the declaration
of a reference other than a non-static data member
or of an object.
The variable's name, if any, denotes the reference or object.

This means that for example:

int a;      // this is a variable that denotes an object of type int.  
int& b=a;   // this is a variable that denotes a reference to an object

The tricky point, is that variables that denote an object give a name to an object stored in memory. But variables that denote a reference might not directly refer to any stored value at all. Some people imagine the reference as a kind of pointer that is stored and automatically dereferenced; but even if implementations cope with it like that, it doesn't have to be: the standard does not give any guarantee that a reference needs its own storage.

The key point here is the word " denotes " means that it gives a name to an object or a reference. It does not at all say that a variable " is " an object.

What does cppreference say?

cppreference wraps this up in a nice article introductory article. The wording doesn't say it's a named object. It says that the variable has a name and this name can be associated with a place in memory:

In C++, a variable is actually just a bit of memory that has been reserved for the program's use. You refer to it using a variable name, so you don't need to worry about where it is in memory

It's not as precise as the standard here. It doesn't speak at all of the special case of the reference variable. But what it says is also true for reference variable. In my example above you may use the identifier b and it will be associated with the place in memory where a is stored.

This describes the same relation between the variable name and the object as "denote" in the standard.

Unrelated: The only criticism I would have about this cppreference article is that introduces variables as being opposed to constants. This seems confusing, since a C++ variable can be constant. It should better oppose the variables to literals.

variable is a kind of name so compiler will see it like a name of memory address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM