The meaning of both eludes me.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Declaration :
Thus declaration associates the variable with a type.
Following are some examples of declaration.
Now function declaration :
Note the semicolon at the end of function so it says it is only a declaration. Compiler knows that somewhere in the program that function will be defined with that prototype. Now if the compiler gets a function call something like this
Compiler will throw an error saying that there is no such function. Because it doesn't has any prototype for that function.
Note the difference between two programs.
Program 1
In this, print function is declared and defined as well. Since function call is coming after the definition. Now see the next program.
Program 2
It is essential because function call precedes definition so compiler must know whether there is any such function. So we declare the function which will inform the compiler.
Definition :
This part of defining a function is called Definition. It says what to do inside the function.
Now with the variables.
Some times declaration and definition are grouped into a single statement like this.
From wiki.answers.com:
The term declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user defined type or function in your program. No space is reserved in memory for any variable in case of declaration. However compiler knows how much space to reserve in case a variable of this type is created.
for example, following are all declarations:
Definition on the other hand means that in additions to all the things that declaration does, space is also reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION" following are examples of definition:
see Answers.
Couldnt you state in the most general terms possible, that a declaration is an identifier in which no storage is allocated and a definition actually allocates storage from a declared identifier?
One interesting thought - a template cannot allocate storage until the class or function is linked with the type information. So is the template identifier a declaration or definition? It should be a declaration since no storage is allocated, and you are simply 'prototyping' the template class or function.
C++11 Update
Since I don't see an answer pertinent to C++11 here's one.
A declaration is a definition unless it declares a/n:
enum X : int;
template<typename T> class MyArray;
int add(int x, int y);
using IntVector = std::vector<int>;
static_assert(sizeof(int) == 4, "Yikes!")
;
Additional clauses inherited from C++03 by the above list:
int add(int x, int y);
extern int a;
orextern "C" { ... };
class C { static int x; };
struct Point;
typedef int Int;
using std::cout;
using namespace NS;
A template-declaration is a declaration. A template-declaration is also a definition if its declaration defines a function, a class, or a static data member.
Examples from the standard which differentiates between declaration and definition that I found helpful in understanding the nuances between them:
The concept of Declaration and Definition will form a pitfall when you are using the extern storage class because your definition will be in some other location and you are declaring the variable in your local code file (page). One difference between C and C++ is that in C you the declarations are done normally at the beginning of a function or code page. In C++ it's not like that. You can declare at a place of your choice.
There are interesting edge cases in C++ (some of them in C too). Consider
That can be a definition or a declaration, depending on what type
T
is:In C++, when using templates, there is another edge case.
The last declaration was not a definition. It's the declaration of an explicit specialization of the static member of
X<bool>
. It tells the compiler: "If it comes to instantiatingX<bool>::member
, then don't instantiate the definition of the member from the primary template, but use the definition found elsewhere". To make it a definition, you have to supply an initializer