C++ 初始化、定义、声明变量的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23345554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The differences between initialize, define, declare a variable
提问by Tony
回答by Shoe
Declaration
宣言
Declaration, generally, refers to the introduction of a new name in the program. For example, you can declarea new function by describing it's "signature":
声明,一般是指在程序中引入一个新名称。例如,您可以通过描述它的“签名”来声明一个新函数:
void xyz();
or declare an incomplete type:
或声明一个不完整的类型:
class klass;
struct ztruct;
and last but not least, to declare an object:
最后但并非最不重要的是,声明一个对象:
int x;
It is described, in the C++ standard, at §3.1/1 as:
它在 C++ 标准的 §3.1/1 中被描述为:
A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations.
声明(第 7 条)可以将一个或多个名称引入翻译单元或重新声明由先前声明引入的名称。
Definition
定义
A definition is a definition of a previously declared name (or it can be both definition and declaration). For example:
定义是对先前声明的名称的定义(或者它可以既是定义又是声明)。例如:
int x;
void xyz() {...}
class klass {...};
struct ztruct {...};
enum { x, y, z };
Specifically the C++ standard defines it, at §3.1/1, as:
具体而言,C++ 标准在 §3.1/1 中将其定义为:
A declaration is a definition unless it declares a function without specifying the function's body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function- body, it declares a static data member in a class definition (9.2, 9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition, or it is a typedef declaration (7.1.3), an alias-declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attribute- declaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).
声明是一个定义,除非它声明了一个没有指定函数体的函数(8.4),它包含 extern 说明符(7.1.1)或链接规范25(7.5)并且既不是初始化器也不是函数体,它声明了一个类定义(9.2、9.4)中的静态数据成员,它是一个类名声明(9.1),它是一个不透明枚举声明(7.2),它是一个模板参数(14.1),它是一个参数-函数声明符中的声明 (8.3.5) 不是函数定义的声明符,或者是 typedef 声明 (7.1.3)、别名声明 (7.1.3)、使用声明 (7.3. 3)、静态断言声明(第 7 条)、属性声明(第 7 条)、空声明(第 7 条)或 using 指令(7.3.4)。
Initialization
初始化
Initialization refers to the "assignment" of a value, at construction time. For a generic object of type T
, it's often in the form:
初始化是指在构造时对值进行“赋值”。对于 type 的通用对象T
,它通常采用以下形式:
T x = i;
but in C++ it can be:
但在 C++ 中它可以是:
T x(i);
or even:
甚至:
T x {i};
with C++11.
使用 C++11。
Conclusion
结论
So does it mean definition equals declaration plus initialization?
那么这是否意味着定义等于声明加初始化?
It depends. On what you are talking about. If you are talking about an object, for example:
这取决于。关于你在说什么。如果您正在谈论一个对象,例如:
int x;
This is a definition without initialization. The following, instead, is a definition with initialization:
这是一个没有初始化的定义。相反,以下是带有初始化的定义:
int x = 0;
In certain context, it doesn't make sense to talk about "initialization", "definition" and "declaration". If you are talking about a function, for example, initializationdoes not mean much.
在某些情况下,谈论“初始化”、“定义”和“声明”是没有意义的。例如,如果你在谈论一个函数,初始化并没有多大意义。
So, the answer is no: definition does not automatically mean declaration plus initialization.
所以,答案是否定的:定义并不自动意味着声明加上初始化。
回答by Lightness Races in Orbit
Declaration says "this thing exists somewhere":
声明说“这东西存在于某处”:
int foo(); // function
extern int bar; // variable
struct T
{
static int baz; // static member variable
};
Definition says "this thing exists here; make memory for it":
定义说“这东西存在于此;为它留出记忆”:
int foo() {} // function
int bar; // variable
int T::baz; // static member variable
Initialisation is optional at the point of definition for objects, and says "here is the initial value for this thing":
初始化在对象的定义点是可选的,并表示“这是这个东西的初始值”:
int bar = 0; // variable
int T::baz = 42; // static member variable
Sometimes it's possible at the point of declaration instead:
有时可以在声明点改为:
struct T
{
static int baz = 42;
};
…but that's getting into more complex features.
……但这涉及到更复杂的功能。
回答by Crowman
For C, at least, per C11 6.7.5:
对于 C,至少,根据 C11 6.7.5:
A declaration specifies the interpretation and attributes of a set of identifiers. A definitionof an identifier is a declaration for that identifier that:
for an object, causes storage to be reserved for that object;
for a function, includes the function body;
for an enumeration constant, is the (only) declaration of the identifier;
for a typedef name, is the first (or only) declaration of the identifier.
声明指定了一组标识符的解释和属性。标识符的定义是对该标识符的声明:
对于一个对象,导致为该对象保留存储空间;
对于函数,包括函数体;
对于枚举常量,是标识符的(唯一)声明;
对于 typedef 名称,是标识符的第一个(或唯一)声明。
Per C11 6.7.9.8-10:
根据 C11 6.7.9.8-10:
An initializer specifies the initial value stored in an object ... if an object that has automatic storage is not initialized explicitly, its value is indeterminate.
初始化器指定存储在对象中的初始值……如果具有自动存储的对象未显式初始化,则其值是不确定的。
So, broadly speaking, a declaration introduces an identifier and provides information about it. For a variable, a definition is a declaration which allocates storage for that variable.
因此,从广义上讲,声明引入了一个标识符并提供了有关它的信息。对于变量,定义是为该变量分配存储空间的声明。
Initialization is the specification of the initial value to be stored in an object, which is not necessarily the same as the first time you explicitly assigna value to it. A variable has a value when you define it, whether or not you explicitly give it a value. If you don't explicitly give it a value, and the variable has automatic storage, it will have an initial value, but that value will be indeterminate. If it has static storage, it will be initialized implicitly depending on the type (e.g. pointer types get initialized to null pointers, arithmetic types get initialized to zero, and so on).
初始化是要存储在对象中的初始值的规范,这不一定与您第一次显式为其赋值相同。一个变量在定义时就有一个值,无论你是否明确地给它一个值。如果你没有明确地给它一个值,并且变量有自动存储,它会有一个初始值,但那个值是不确定的。如果它有静态存储,它将根据类型隐式初始化(例如,指针类型被初始化为空指针,算术类型被初始化为零,等等)。
So, if you define an automatic variable without specifying an initial value for it, such as:
因此,如果您定义了一个自动变量而不为其指定初始值,例如:
int myfunc(void) {
int myvar;
...
You are defining it (and therefore also declaring it, since definitions are declarations), but not initializing it. Therefore, definition does not equal declaration plus initialization.
您正在定义它(因此也声明它,因为定义是声明),而不是初始化它。因此,定义不等于声明加初始化。
回答by Shoaib
"So does it mean definition equals declaration plus initialization."
“所以这是否意味着定义等于声明加初始化。”
Not necessarily, your declaration might be without any variable being initialized like:
不一定,您的声明可能没有初始化任何变量,例如:
void helloWorld(); //declaration or Prototype.
void helloWorld()
{
std::cout << "Hello World\n";
}