What exactly is a variable in C++?

给你一囗甜甜゛ 提交于 2020-01-11 02:05:07

问题


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


回答1:


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.".




回答2:


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




回答3:


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




回答4:


As I have learned

What are variables

A variable is an identifier that is bind to a value stored in the system's memory(imperative languages) or an expression that can be evaluated(functional languages).

Why we need variables

  • provide names for storage chunks to store data temporarily during the lifespan of a computer program.
  • are also used in programming to transfer information from one part of the program to another part(Eg: parameters, global variables).



回答5:


In mathematics, a variable is a symbol used to stand in for a value of a certain kind when we don't know, don't care about, or don't want to commit to the particular value we're dealing with exactly. It's a name used to represent a variable value of some kind…

Similarly, in computer programming, a variable is a symbol used to represent a value of some kind. Given the history of computer science, it seems pretty safe to say that the use of the term in programming is directly derived from its use in mathematics. The whole point of variables is that the value they represent is variable, i.e., may vary. Using variables, we can express some computation involving values of some sort without necessarily having to know what these values are going to be exactly. In a way, variables are how we separate the description of what computation to carry out from the concrete data that computation is carried out on…

As far as C++ is concerned, you seem to have already stumbled upon the definition the C++ standard works with [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.

Since references are not objects (if they were, there'd be no point in making that distinction in the sentence quoted above), it is technically not 100% correct to say that a variable "is an object paired with an identifier". A declaration that introduces a variable can also create an object [intro.object]/1. A variable of object type denotes an object. However, while a variable of reference type effectively ends up doing that as well [expr]/5, the variable itself actually denotes the reference, not the object. And there can be references to functions, which neither are nor refer to objects yet still are variables. And since variables that are references are not objects and don't have addresses, there is not necessarily any memory location that directly corresponds to such a variable itself…

But even in light of all these technicalities, if you just consider the behavior it all boils down to in the end, a variable is simply a symbolic name given to represent some kind of value…




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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




回答10:


Here's the definition from the C++17 standard:

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.

My take on this, quite frankly, is that that's not really a definition. It tells us what introduces a variable, but it doesn't define what a variable is.

Consider, for example:

int foo = 42;

This is a declaration of an object, so it "introduces" a variable. But what is the variable? Is the object named foo a variable? I would have thought so, but the definition doesn't actually say that. Apparently the "variable" has a name (in this case "foo") and that name denotes the object. Since it has a name, presumably the name itself is not the variable. And it would have been easy enough to say that the variable is the object, rather than that the variable's name denotes the object, if that were the intent.

What is a "variable" in C++? I really don't know, and I don't believe it's possible to answer the question based on the wording in the standard. (And I'd like that to be corrected in a future edition.)

(The C standard deals with this by not defining "variable" and, for the most part, not using it except as an adjective or informally.)



来源:https://stackoverflow.com/questions/2920773/what-exactly-is-a-variable-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!