C++ 到底是什么? 定义、声明和赋值?
我倾向于交替使用“定义”、“声明”和“分配”这三个词,但这似乎会冒犯某些人。 这合理吗? 我应该只在第一次分配给变量时使用“声明”一词吗? 或者还有比这更多的事情吗?
I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for the first time I assign to a variable? Or is there more to it than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用正确的术语很重要,否则人们将不知道你在说什么,或者错误地认为你不知道你在说什么。
It is important to use the correct terminology, otherwise people will not know what you are talking about, or incorrectly assume that you don't know what you are talking about.
这些术语在各种语言的标准中通常具有精确的含义。 在这种情况下,不应将它们混为一谈。
例如,在 c 中:
一个函数可能仅定义一次(当你说出它的作用时),但它也可能在此之前声明(当你说它接受什么参数以及它返回什么类型)。
同样,当您说出变量的类型时,声明了变量,并且每个作用域仅发生一次。 但您可以重复分配一个值。 (某些语言还区分初始化(在声明时为变量赋予值)和赋值(稍后更改值)。)
These terms often have precise meanings in the standards for various languages. When that is the case they should not be conflated.
In c for instance:
a function may be defined only once (when you say what it does), but it may also be declared before that (when you say what arguments it takes and what type it returns).
likewise a variable is declared when you say what type it is, and this happens only once for each scope. But you may assign a value repeatedly. (Some languages also differentiate between initialization (giving a variable a value at declaration time) and assignment (changing the value later).)
定义是描述值或函数的地方,即编译器或程序员被准确地告知它是什么,例如
声明告诉编译器或程序员该函数或变量存在。 例如,
赋值是指为变量设置值,通常使用 = 运算符。 例如
A definition is where a value or function is described, i.e. the compiler or programmer is told precisely what it is, e.g.
A declaration tells the compiler, or programmer that the function or variable exists. e.g.
An assignment is when a variable has its value set, usually with the = operator. e.g.
定义和声明很相似,但分配却有很大不同。
此处我声明(或定义)一个变量:
此处我为该变量赋值:
此处我在一条语句中执行这两项操作:
注意
并非所有语言都支持在一条语句中进行声明和赋值:
< T-SQL
某些语言要求您在声明时为变量赋值。 此要求允许语言的编译器或解释器推断变量的类型:
Python
Define and declare are similar but assign is very different.
Here I am declaring (or defining) a variable:
Here I am assigning a value to that variable:
Here I am doing both in one statement:
Note
Not all languages support declaration and assignment in one statement:
T-SQL
Some languages require that you assign a value to a variable upon declaration. This requirement allows the compiler or interpreter of the language to infer a type for the variable:
Python
一般角色:
定义=声明+保留空间。
定义、声明、赋值有两种情况:
对于变量:
-- 定义:
告诉编译器为变量保留内存。
-- 声明:
告诉编译器该变量在其他地方定义。
-- 作业:
告诉编译器将值放入变量中。
对于函数:
-- 定义:
-- 声明:
这只是函数的原型。
General Role:
Definition = declaration + reserved space.
Definition, declaration, and assignment have two cases:
For Variables:
-- Definition:
To tell the compiler to reserve memory for the variable.
-- Declaration:
To tell the compiler that the variable defined in somewhere else.
-- Assignment:
To tell the compiler to put the value in the variable.
For Functions:
-- Definition:
-- Declaration:
It is just the prototype of the function.
这些差异看似微妙,但却很重要。 并非每种语言都有相同的区别,但在 C++ 中,变量声明使编译器知道变量的类型和名称
变量定义分配存储空间并指定初始值对于变量。
您可以将变量声明和定义合并到一个语句中,就像通常所做的那样。
在函数内部声明变量也会为该变量预留内存,因此以下代码隐式地将变量
a
定义为其声明的一部分。由于变量
a
是由编译器自动定义的,因此它将包含为其分配的内存位置中的任何值。 这就是为什么在明确为自动变量分配已知值之前使用自动变量是不安全的。每当您更改程序中的变量值时,都会发生赋值。
与变量声明类似,函数声明使编译器知道函数签名。 这允许您在定义函数之前调用源代码中的函数,而不会导致编译器错误。
函数定义指定函数的返回类型、名称、参数列表和指令。 这些元素的前三个必须与函数声明匹配。 函数在给定程序中只能定义一次。
您可以将函数声明和定义合二为一,但必须在程序中的任何位置调用该函数之前执行此操作。
The differences can seem subtle, but they are important. Not every language makes the same distinctions, but in C++ a variable declaration makes the type and name of the variable known to the compiler
A variable definition allocates storage and specifies an initial value for the variable.
You can combine a variable declaration and definition into one statement, as is commonly done.
Declaring a variable inside a function will also set aside memory for the variable, so the following code implicitly defines variable
a
as a part of its declaration.Since variable
a
is automatically defined by the compiler, it will contain whatever value was in the memory location that was allocated for it. This is why it is not safe to use automatic variables until you've explicitly assigned a known value to them.An assignment takes place any time you change the value of a variable in your program.
A function declaration, similar to the variable declaration, makes the function signature known to the compiler. This allows you to call a function in your source code before it is defined without causing a compiler error.
A function definition specifies the return type, name, parameter list, and instructions for a function. The first three of these elements must match the function declaration. A function must only be defined once in a given program.
You can combine the function decalartion and definition into one, but you must do so before the function is called anywhere in your program.
正如前面所说,这可能取决于语言。 我认为这实际上取决于这些词是否用于诸如课程之类的东西。 对于这里讨论的大多数数据类型,这个问题可能没有太大的相关性。 在 C++ 中(请参阅 c++ - a 和 a 之间有什么区别定义和声明?),类或结构总是只有一个定义,但可以声明零次或多次。 没有定义就不能声明类。 因此“声明”可能与“使用”同义。
在大多数语言中,简单类型(例如整数)不需要像类那样的定义。
It might depend on the language, as has been said. I think it really depends on whether the words are used for things like classes. For most of the data types discussed here, the question might not have much relevance. In C++ (see c++ - What is the difference between a definition and a declaration?), a class or struct always has precisely one definition but can be declared zero or more times. A class cannot be declared without a definition. So "declared" might be synonymous with "used".
In most languages, simple types such as integers do not need definitions in the manner that classes do.
正确答案取决于您所谈论的语言。 计算机语言通常有特定的术语,要么是因为语言规范,要么是因为围绕该语言发展起来的社区。 当我使用 COBOL 时,它的术语与更主流的语言有很大不同(在更接近主流语言开发的语言的意义上,而不是主流业务的语言)。 福斯发展了一些奇怪的术语。
如果您懂英语,通常可以从单词的正常含义中很好地了解其含义,但永远不要过分依赖它。 对于跨语言或语言社区的特定单词也是如此。
The correct answer depends on which language you're talking about. Computer languages often have specific terminology, either because of the language specification or the community grown up around the language. COBOL, back when I used it, had a much different terminology than more mainstream languages (in the sense of languages closer to the mainstream of language development, not mainstream business). Forth developed some strange terminology.
If you know English, you can usually get a good idea as to what a word means from its normal meaning, but never count on it too much. The same is true with specific words across languages or language communities.