我不明白两者的意义。


当前回答

声明是在未分配值或对象的情况下创建原语或对象引用变量或方法。 int; Final int a;

定义意味着分别为值或对象赋值 Int a =10;

初始化意味着为各个变量或对象分配内存。

其他回答

从c++标准文档中添加定义和声明示例(来自3.1节声明和定义)

定义:

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

声明:

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 d

c++中有一些有趣的边缘情况(在C中也有一些)。考虑

T t;

它可以是一个定义,也可以是一个声明,取决于T是什么类型:

typedef void T();
T t; // declaration of function "t"

struct X { 
  T t; // declaration of function "t".
};

typedef int T;
T t; // definition of object "t".

在c++中,当使用模板时,还有另一种边缘情况。

template <typename T>
struct X { 
  static int member; // declaration
};

template<typename T>
int X<T>::member; // definition

template<>
int X<bool>::member; // declaration!

最后一个声明不是定义。它声明了X<bool>的静态成员的显式特化。它告诉编译器:“如果要实例化X<bool>::member,那么不要从主模板实例化成员的定义,而是使用在其他地方找到的定义”。要使其成为定义,必须提供初始化式

template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.

c++标准3.1节:

声明将名称引入到翻译单元中,或者重新声明前一个翻译单元中引入的名称 声明。声明指定这些名称的解释和属性。

下一段声明(强调是我的)声明是一种定义,除非……

... 它声明一个函数而不指定函数体:

void sqrt(double);  // declares sqrt

... 它在类定义中声明一个静态成员:

struct X
{
    int a;         // defines a
    static int b;  // declares b
};

... 它声明了一个类名:

class Y;

... 它包含没有初始化式或函数体的extern关键字:

extern const int i = 0;  // defines i
extern int j;  // declares j
extern "C"
{
    void foo();  // declares foo
}

... Or是类型定义或using语句。

typedef long LONG_32;  // declares LONG_32
using namespace std;   // declares std

现在,理解声明和定义之间的区别很重要的一个重要原因是:一个定义规则。c++标准第3.2.1节:

任何翻译单元都不能包含任何变量、函数、类类型、枚举类型或模板的多个定义。

在这里找到类似的答案:C语言的技术面试问题。

声明为程序提供了一个名称;定义提供程序中实体(例如类型、实例和函数)的唯一描述。声明可以在给定的作用域中重复,它在给定的作用域中引入一个名称。

声明是定义,除非:

声明声明了一个函数,但没有指定函数体, 声明包含一个extern说明符,没有初始化式或函数体, 声明是没有类定义的静态类数据成员的声明, 声明是类名定义,

定义是声明,除非:

定义定义一个静态类数据成员, Definition定义一个非内联成员函数。

从wiki.answers.com:

术语声明(在C语言中)意味着你告诉编译器关于类型,大小,如果是函数声明,任何变量的参数的类型和大小,或者程序中用户定义的类型或函数。在声明时,内存中不为任何变量保留空间。然而,编译器知道在这种类型的变量被创建的情况下要保留多少空间。

例如,以下是所有的声明:

extern int a; 
struct _tagExample { int a; int b; }; 
int myFunc (int a, int b);

另一方面,定义意味着除了声明所做的所有事情之外,内存中还保留了空间。你可以说“DEFINITION = DECLARATION + SPACE RESERVATION”,下面是定义的例子:

int a; 
int b = 0; 
int myFunc (int a, int b) { return a + b; } 
struct _tagExample example; 

看到答案。