C ++中的变量到底是什么? [英] What exactly is a variable in C++?
问题描述
标准说
变量是通过对象的声明引入的.变量的名称表示对象.
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?
为了进一步混淆,许多C ++书籍似乎将变量和对象视为同义词.
To confuse things further, many C++ books seem to treat variables and objects as synonyms.
您对此有何看法?
关于实体,引用C ++ 0x草案:
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
new int // create one int object
std::string() // create one string object
下面的代码创建一个名称为"foo"的数组变量和5个类型为"int"的未命名(子)对象
The following creates one array variable with name "foo" and 5 unnamed (sub-) objects of type "int"
int foo[5];
以下不是C ++ 03中的变量,但已成为C ++ 0x中的变量(声明的引用是C ++ 0x中的变量,有关详细信息,请参见链接)
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;
变量是否给对象命名,即变量是否只是匿名对象的命名机制?
变量是对象(或分别是引用).C ++的实体列表(在C ++ 03中为 3/3
)包含多个这样的
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.
C ++ 0x的实体列表对我来说看起来更干净一些,它不包含变量",函数实例"(这种实体甚至对我来说都不是显而易见的),子对象"和数组元素"了.相反,它添加了模板专业化",它们可以是函数,类或模板(部分专业化).
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).
1.8
中的C ++对象模型说
The C++ object model at 1.8
says
一个对象可以具有一个名称(第3节).
因此,如果您愿意,可以将语句表述为对象的名称表示对象".
So if you like, you can formulate the statement as "The object's name denotes the object.".
这篇关于C ++中的变量到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!