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.
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>
usingnamespace std;
class Foo
{
public:
staticconstint 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.
#include <iostream>
usingnamespace std;
class Foo
{
public:
staticconstint bar = 180; // declared and initialized
with initializer
};
constint 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 inititlizationintitializer from the declaration of Foo::bar to the definition; but it can only be in one of the two places.
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.]
struct A
{
A() ;
A( int ii ) ;
A( int ii, int jj ) ;
constint i = 8 ; // declared with an initializer
constint j = 9 ; // declared with an initializer
staticconstint s = 78 ; // declared with an initializer
// A::s can appear in constant expressions.
};
constint 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