extern int bar;extern int g(int, int);double f(int, double); // extern can be omitted for function declarationsclass foo; // no extern allowed for type declarations
包含声明或链接说明符的extern说明符-extern int a;或extern "C" { ... };
类中的静态数据成员-x inclass C { static int x; };
类/结构声明-struct Point;
typedef声明-typedef int Int;
使用声明-using std::cout;
使用指令-using namespace NS;
模板声明是一个声明。如果模板声明定义了函数、类或静态数据成员,则模板声明也是一个定义。
标准中区分声明和定义的示例,我发现这些示例有助于理解它们之间的细微差别:
// except one all these are definitionsint a; // defines aextern const int c = 1; // defines cint f(int x) { return x + a; } // defines f and defines xstruct S { int a; int b; }; // defines S, S::a, and S::bstruct X { // defines Xint x; // defines non-static data member xstatic int y; // DECLARES static data member yX(): x(0) { } // defines a constructor of X};int X::y = 1; // defines X::yenum { up , down }; // defines up and downnamespace N { int d; } // defines N and N::dnamespace N1 = N; // defines N1X anX; // defines anX
// all these are declarationsextern int a; // declares aextern const int c; // declares cint f(int); // declares fstruct S; // declares Stypedef int Int; // declares Intextern X anotherX; // declares anotherXusing N::d; // declares N::d
// specific to C++11 - these are not from the standardenum X : int; // declares X with int as the underlying typeusing 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 exprtemplate <class T> class C; // declares template class C; // declares nothing
int a; // defines aextern const int c = 1; // defines cint f(int x) { return x+a; } // defines f and defines xstruct S { int a; int b; }; // defines S, S::a, and S::bstruct X { // defines Xint x; // defines non-static data member xstatic int y; // DECLARES static data member yX(): x(0) { } // defines a constructor of X};int X::y = 1; // defines X::yenum { up, down }; // defines up and downnamespace N { int d; } // defines N and N::dnamespace N1 = N; // defines N1X anX; // defines anX
声明:
extern int a; // declares aextern const int c; // declares cint f(int); // declares fstruct S; // declares Stypedef int Int; // declares Intextern X anotherX; // declares anotherXusing N::d; // declares d