Can I get an explanation of declaration, definition and initialization?

It always confuses me because they can be happening at the same time, so what does it all actually mean? my limited knowledge is:

Declaration is telling the compiler that something exists
Definition is telling the compiler what that something is
Initialization is giving(specifically or otherwise) a variable a value when it is first defined.

so

 
int i = 10;


would be all 3, where as a function prototype

 
void func2(int a);


would be declaration.

Is that correct? and is

 
extern int i;


only a declaration?
Last edited on


int i = 10; // definition

void func2(int a); // declaration

extern int i; // declaration

Note that definitions are also always declarations. And obviously, one cannot initialize something that is not also being defined.
And obviously, one cannot initialize something that is not also being defined.
Edit: reworked, after reading JLBorges post below, in an attempt to correctly distinguish the provision of an initializer from the actual initialization.

With the exception of const static class members.

In C++03 this is restricted to integral and enum types; C++11 provides a good bit of additional support for this mechanism when you use constexpr rather than const.

Here Foo::bar is being declared and initialized in-class but not defined. You get away without the definition here as the compiler just needs to know the value.


One special case is the declaration of class members. In this case you can provide an initializer as part of the declatation; but the actual initialization still happens at the point the member is defined.

In C++03 this is restricted to static const members of integral and enum types; C++11 extends this mechanism to also support non-const members.

Here Foo::bar is being declared and initialized (with an initializer) in-class but not defined. You get away without the definition here as the compiler just needs to know the value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

class Foo
{
public:
    static const int bar = 180; // declared and initialized 

with initializer
};

int main()
{
    cout << "Foo::bar = " << Foo::bar<< "\n"; // just use value

    return 0;
}


But if you use the address of Foo::bar, then you do need it to be defined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

class Foo
{
public:
    static const int bar = 180; // declared and initialized 

with initializer
};

const int Foo::bar; // defined but not initialized 

(initialization happens here, not where declared)
// needed when using address, or you get the linker
// error : "undefined reference to `Foo::bar'"

int main()
{
    cout << "Foo::bar = " << Foo::bar<< "\n"; // just use value
    cout << "addr of Foo::bar = " << &Foo::bar<< "\n"; // use address

    return 0;
}


You can move the inititlization intitializer from the declaration of Foo::bar to the definition; but it can only be in one of the two places.

Andy
Last edited on
closed account (z05DSL3A)
Zephilinox wrote:
Declaration is telling the compiler that something exists
Definition is telling the compiler what that something is
Initialization is giving(specifically or otherwise) a variable a value when it is first defined.

I'm not sure that I agree with this.

Declaration: is giving an identifier attributes and telling the compiler/reader how to interpret the identifier.
[Edit: In other words:if it does exist it looks like this]

Definition: is giving a declaration form. causing storage to be assigned, giving the function a body...
[Edit: In other words: it does exist, here it is.]

Initialization: is giving it its first value.
Last edited on
@Grey Wolf +1

> With the exception of const static class members.
...
>
1
2
3
4
5
class Foo
{
public:
    static const int bar = 180; // declared and initialized
};

> Here Foo::bar is being declared and initialized in-class but not defined.

The declaration of Foo::bar specifies a brace-or-equal-initializer; that does not imply that it is being initialized at the point of declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct A
{
    A() ;
    A( int ii ) ;
    A( int ii, int jj ) ;

    const int i = 8 ; // declared with an initializer
    const int j = 9 ; // declared with an initializer

    static const int s = 78 ; // declared with an initializer
    // A::s can appear in constant expressions.
};

const int A::s ; // A::s is defined and initialized with 78

A::A() {} // A::i is initialized with 8, A::j initialized with 9

A::A( int ii ) : i(ii) {} // A::i is initialized with ii,
                          // A::j initialized with 9

A::A( int ii, int jj ) : i(ii), j(jj) {} // A::i is initialized with ii,
                                          // A::j initialized with jj 

Sorry to derail here but what does 'Foo' and 'bar' mean? I see that used a lot.
Last edited on
> what does 'Foo' and 'bar' mean?

http://www.cplusplus.com/forum/general/62926/
thanks
@JLBorges

does not imply that it is being initialized at the point of declaration.

Thanks for the clarification.

Reading you post it was obvious that you can't actually initialize something before it exists!

Have attempted to clarify my post...

Andy
Topic archived. No new replies allowed.