初始化之间的差异,定义,声明一个变量 [英] The differences between initialize, define, declare a variable

查看:110
本文介绍了初始化之间的差异,定义,声明一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看完后的<一个href=\"http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration\">question,我知道,声明和定义之间的差异。所以,它的意思是定义等于申报加初始化?

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();

或声明的不完全类型:

class klass;
struct ztruct;

和最后但并非最不重要的,申报对象:

and last but not least, to declare an object:

int x;

据介绍,在C ++标准,在3.1节/ 1为:

It is described, in the C++ standard, at §3.1/1 as:

一个声明(第7章)可能会引入一个或多个名称到一个翻译单元或previous声明介绍重新声明的名称。

A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations.

一个定义为previously声明的名称的定义(或者它可以是既定义和声明)。例如:

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

具体的C ++标准,它定义在3.1节/ 1为:

Specifically the C++ standard defines it, at §3.1/1, as:

一个声明是一个定义,除非它声明了一个功能,而无需指定函数体(8.4),它包含extern声明符(7.1.1)或联动specification25(7.5),并没有一个初始化,也不是一个功能 - 身体,它声明一个类定义(9.2,9.4),它是一个类名的声明(9.1)静态数据成员,它是一个不透明枚举声明(7.2),它是一个模板参数(14.1),它是在声明符,一个函数的参数声明(8.3.5)不是一个函数定义的声明符,或者它是一个typedef声明(7.1.3),别名声明(7.1.3),一个using声明(7.3.3),一个static_assert声明(第7条),一个属性 - 声明(第7条),一个空的声明(第7条),或使用指示符(7.3.4)。

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

初​​始化指的是一个价值的分配,在施工时间。对于类型 T 的通用对象,它往往形式:

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 x = i;

但在C ++中,它可以是:

but in C++ it can be:

T x(i);

甚至是:

T x {i};

与C ++ 11。

with C++11.

那么是否意味着定义等于申报加初始化?

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.

这篇关于初始化之间的差异,定义,声明一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆