Broad Network


Difference Between Declaration and Definition in C++

Some Features of C++ Entities – Part 3

Forward: In this part of the series, I explain the difference between Declaration and Definition in C++.

By: Chrysanthus Date Published: 26 Aug 2012

Introduction

This is part 3 of my series, Some Features of C++ Entities. In this part of the series, I explain the difference between Declaration and Definition in C++.

Meaning of Declaration and Definition
A C++ program consists of declarations. However, if a declaration allocates an area of memory, it is called a definition. I will spend the rest of this tutorial illustrating with some examples. The examples are taken from the C++ ISO/IEC 14882:2003(E) specification. The explanations below the list are mine but the examples are from the specification.

Examples of Definitions and Declarations
All but one of the following 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 nonstatic 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; } // definesN and N::d
    namespace N1 = N; // defines N1
    X anX; // defines anX

The followings are just 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

Explanation to Examples
The explanations of the definitions (and the one declaration) in the first list are as follows:

    int a; // defines a
This is a definition because an area of memory is allocated for the int object identified by a. It does not matter what the value will be in the region (area) of memory; the size and position of the region will always be the same. The declaration of any fundamental object type allocates memory of a particular size.

    extern const int c = 1; // defines c
Here, there is initialization. Initialization means, a region of memory has already been allocated with a value. The presence of the value raises no doubt as to whether or not an area (region) of memory has been allocated.

    int f(int x) { return x+a; } // defines f and defines x
Here, “int x” allocates a region of memory for the object identified by x. In my computer the size of an int is 4 bytes long (consecutive memory cells). The construct is the definition of a function. The block of the function has one statement (it can have more). The statement or statements are recorded in memory and so occupy memory cells. So memory has been allocated, making the function description, a definition. A function prototype is not a definition, as it does not really allocate memory for the whole function.

    struct S { int a; int b; }; // defines S, S::a, and S::b
This is a struct with its block description. The statements in the block are placed in memory and they occupy space in the memory. So the struct and its description is a definition. S::a, and S::b allocate memory for two integers of the same size (see above).

    struct X { // defines X
        int x; // defines nonstatic data member x
        static int y; // declares static data member y
        X(): x(0) { } // defines a constructor of X
    };

This is another struct with its description. The statements in the block are placed in memory and they occupy space in memory. That alone makes the description of the struct a definition. Inside the struct, you have “int x;”, which allocates memory for any potential value that is an int. The region of memory is identified by x. Inside the struct you also have, “static int y;”. Know that a static identifier inside a struct or class, without initialization does not really allocate a definite memory region for the object; so this is just a declaration and not a definition. The struct also has, “X(): x(0) { }”, which is a constructor function. Even though the block of the constructor function has no statement, definite (little) memory has still been allocated. So the constructor and its block is a definition.

    int X::y = 1; // defines X::y
This involves allocation of memory and initialization: definitely a definition.

    enum { up, down }; // defines up and down
The block here has statements that occupy space in memory; so the enum description is a definition.

    namespace N { int d; } // definesN and N::d

The block here has statements that occupy space in memory; so the namespace description is a definition.

    namespace N1 = N; // defines N1

N1 is a copy of N above, which is a definition; so N1 has its own memory space and so is a definition.

    X anX; // defines anX
This is instantiation. Instantiation of a struct or class is a definition because that action creates an object in memory. Any object, be it fundamental or instantiated, occupies a region in memory; and so any object (creation) is a definition.

The explanations of the declarations are as follows:

    extern int a; // declares a
The extern identifier without initialization does not really allocate memory; so this is a declaration.

    extern const int c; // declares c
The extern const identifier without initialization does not really allocate memory; so this is a declaration.

    int f(int); // declares f
This is a function prototype without the block of statements to be placed in memory; so this is a declaration, as it does not allocate all the memory needed by the function.

    struct S; // declares S
This is like the prototype of a struct without the description block; so it is a declaration, as it does not allocate all the memory needed by the struct.

    typedef int Int; // declares Int
This is telling the compiler or system that Int becomes a synonym for the object type, int. It does not talk about any specified object, as there is no identifier. So, it is just a declaration.

    using N::d; // declares N::d
The using instruction does not allocate memory for any C++ entity or object. So, this is a declaration.

Well, I have explained all the statements (examples) above. I hope you now appreciate the difference between declaration and definition.

We stop here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message