7.3 Declarations versus Definitions

Ru-Brd

So far, the words declaration and definition have been used only a few times in this book. However, these words carry with them a rather precise meaning in standard C++, and that is the meaning that we use.

A declaration is a C++ construct that introduces or reintroduces a name into a C++ scope. This introduction always includes a partial classification of that name , but the details are not required to make a valid declaration. For example:

 class C;  // a declaration of  C  as a class  void f(int p);  // a declaration of  f()  as a function and  p  as a named parameter  extern int v;  // a declaration of  v  as a variable  

Note that even though they have a "name," macro definitions and goto labels are not considered declarations in C++.

Declarations become definitions when the details of their structure are made known or, in the case of variables , when storage space must be allocated. For class type and function definitions, this means a brace - enclosed body must be provided. For variables, initializations and a missing extern lead to definitions. Here are examples that complement the preceding nondefinition declarations:

 class C {};  // definition (and declaration) of class  C  void f(int p) {  // definition (and declaration) of function  f()      std::cout << p << std::endl;  }  extern int v = 1;  // an initializer makes this a definition for  v  int w;  // global variable declarations not preceded by   //  extern  are also definitions  

By extension, the declaration of a class template or function template is called a definition if it has a body. Hence,

 template <typename T>  void func (T); 

is a declaration that is not a definition, whereas

 template <typename T>  class S {}; 

is in fact a definition.

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net