Declaration vs. Definition

What is the difference between a declaration and a definition?

Declaration  tells the compiler that the signature of the identifier is known. The identifier can be representation of variable, function, structure, class, etc. Declaration describe its type and be it a type. Important think is that the momory for the object is not allocated. It’s mean that the object doesn’t exist – we can not get access to it and assign a value. Examples in C++ code:

extern int bar;
extern void foo(int);
double foo(int, double);     // extern can be omitted for function declarations
class foo;     // no extern allowed for class declarations

Definition – allocate area in memory for the identifier which was declared. Definition defines instantiates or implements the identifier. Examples in C++ code:

int bar;
int g(int a) {return a * 2;}
double f(int i, double d) { return i + d; }
class foo {};

Remember:

Each definition is both declaration (but not vice versa).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s