简体   繁体   English

初始化、定义、声明变量的区别

[英]The differences between initialize, define, declare a variable

After reading the question , I know the differences between declaration and definition.阅读问题后,我知道声明和定义之间的区别。 So does it mean definition equals declaration plus initialization?那么这是否意味着定义等于声明加初始化?

Declaration宣言

Declaration, generally, refers to the introduction of a new name in the program.声明,一般是指在程序中引入一个新名称。 For example, you can declare a 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:对于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, initialization does not mean much.例如,如果你在谈论一个函数,初始化并没有多大意义。

So, the answer is no : definition does not automatically mean declaration plus initialization.所以,答案是否定的:定义并不自动意味着声明加上初始化。

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. ……但这涉及到更复杂的功能。

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 definition of 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 assign a 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 (eg 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.因此,定义不等于声明加初始化。

"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";
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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