我不明白两者的意义。


当前回答

变量的声明是为了通知编译器以下信息:变量的名称,它持有的值的类型和初始值(如果有的话)。也就是说,声明给出了关于变量属性的详细信息。然而,变量的定义说的是变量存储在哪里。也就是说,变量的内存是在定义变量时分配的。

其他回答

当您使用extern存储类时,声明和定义的概念将形成一个陷阱,因为您的定义将位于其他位置,而您是在本地代码文件(页面)中声明变量。C和c++之间的一个区别是,在C中,声明通常在函数或代码页的开头完成。在c++中不是这样的。你可以在你选择的地方申报。

我最喜欢的例子是int Num = 5这里你的变量是1。定义为int 2。声明为Num和3。实例化值为5。我们

定义对象的类型,可以是内置的,也可以是类或结构。 声明一个对象的名字,这样任何有名字的东西都被声明了,包括变量、函数等。

类或结构允许您在以后使用时更改对象的定义方式。例如

可以声明没有特别定义的异构变量或数组。 在c++中使用偏移量可以定义一个没有声明名称的对象。

当我们学习编程时,这两个术语经常被混淆,因为我们经常同时做这两个。

声明引入标识符并描述其类型,可以是类型、对象或函数。声明是编译器接受对该标识符的引用所需要的。这些是声明:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations

定义实际上实例化/实现了这个标识符。它是链接器为了将引用链接到这些实体所需要的。这些是对应于上述声明的定义:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

定义可以用在声明的地方。

标识符可以根据需要任意声明。因此,以下代码在C和c++中是合法的:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

但是,它必须只定义一次。如果您忘记定义某个已经声明和引用的东西,那么链接器就不知道要链接到什么地方,并抱怨缺少符号。如果定义一个东西不止一次,那么链接器就不知道要链接引用哪个定义,并抱怨重复的符号。


由于在c++中什么是类声明和类定义的争论不断出现(在对其他问题的回答和评论中),我将在这里粘贴一段来自c++标准的引用。 在3.1/2,c++ 03说:

声明是定义,除非它[…是类名声明[…]。

3.1/3然后给出了几个例子。其中:

[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example

总结一下:c++标准考虑结构体x;是一个声明和结构x {};一个定义。(换句话说,“前向声明”是用词不当,因为在c++中没有其他形式的类声明。)

感谢litb (Johannes Schaub)在他的回答中找出了实际的章节和诗句。

c++ 11更新

由于我没有看到与c++ 11相关的答案,这里有一个。

声明是定义,除非声明了/n:

opaque enum - enum X: int; 模板参数-模板参数- MyArray 参数声明- x和y在int add(int x, int y); 别名声明-使用IntVector = std::vector<int>; - static_assert(sizeof(int) == 4, "Yikes!") 属性声明(实现定义的) 空声明;

以上列表从c++ 03继承的附加子句:

函数声明- add in int add(int x, int y); Extern说明符包含声明或链接说明符- Extern int a;或extern "C"{…}; 类中的静态数据成员-类C中的x{静态int x;}; 类/struct声明- struct Point; typedef int int; 使用声明-使用std::cout; 使用命名空间NS;

模板声明是一种声明。如果模板声明定义了函数、类或静态数据成员,那么模板声明也是定义。

以下例子来自于区分声明和定义的标准,我发现这些例子有助于理解它们之间的细微差别:

// except one all these are definitions
int a;                                  // defines a
extern const int c = 1;                 // defines c
int f(int x) { return x + a; }          // defines f and defines x
struct S { int a; int b; };             // defines S, S::a, and S::b
struct X {                              // defines X
    int x;                              // defines non-static data member x
    static int y;                       // DECLARES static data member y
    X(): x(0) { }                       // defines a constructor of X
};
int X::y = 1;                           // defines X::y
enum { up , down };                     // defines up and down
namespace N { int d; }                  // defines N and N::d
namespace N1 = N;                       // defines N1
X anX;                                  // defines anX


// all these are declarations
extern int a;                           // declares a
extern const int c;                     // declares c
int f(int);                             // declares f
struct S;                               // declares S
typedef int Int;                        // declares Int
extern X anotherX;                      // declares anotherX
using N::d;                             // declares N::d


// specific to C++11 - these are not from the standard
enum X : int;                           // declares X with int as the underlying type
using IntVector = std::vector<int>;     // declares IntVector as an alias to std::vector<int>
static_assert(X::y == 1, "Oops!");      // declares a static_assert which can render the program ill-formed or have no effect like an empty declaration, depending on the result of expr
template <class T> class C;             // declares template class C
;                                       // declares nothing

从C99标准,6.7(5):

声明指定一组标识符的解释和属性。标识符的定义是该标识符的声明:

对于一个对象,使存储空间为该对象保留; 对于函数,包括函数体; 类的(唯一)声明 标识符。

来自c++标准3.1(2):

声明是定义,除非它声明了一个函数而没有指定函数体,它包含extern说明符或链接规范,但既没有初始化式也没有函数体,它在类声明中声明了静态数据成员,它是类名声明,或者它是typedef声明,using-declaration,或using-directive。

下面是一些例子。

有趣的是(也许不是,但我有点惊讶)typedef int myint;是C99中的定义,但在c++中只是声明。