我不明白两者的意义。


当前回答

经验法则:

声明告诉编译器如何解释内存中的变量数据。这是每个访问都需要的。 定义保留内存以使变量存在。这必须在第一次访问之前发生一次。

其他回答

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

宣言

声明告诉编译器a 程序元素或名称存在。一个 声明引入一个或多个 命名到程序中。声明可以 在一个程序中出现一次以上。 因此,类,结构, 枚举类型等 用户定义的类型可以声明为 每个编译单元。

定义

定义指定哪些代码或数据 名称描述。名称必须为 在使用之前声明。

根据GNU C库手册(http://www.gnu.org/software/libc/manual/html_node/Header-Files.html)

在C语言中,声明仅仅提供函数或变量存在的信息并给出其类型。对于函数声明,还可以提供关于其参数类型的信息。声明的目的是允许编译器正确地处理对声明的变量和函数的引用。另一方面,定义实际上为变量分配存储空间,或者说明函数的功能。

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

看到答案。

从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