Declaration
Derleyiciye bir değişken, sınıf veya metodun tipi
bildirilir. Değişken için bellek ayrılmaz."Genelde" header dosyasındaki satırlar declaration olarak tabir edilirler. Declaration örnekleri
şöyle
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 d
Definition
Bazı definition örnekleri
şöyle
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 non-static 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; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
Değişken
Değişken için bellek
ayrılır.
int bar;
Değişken için bellek ayrıldığı aşağıda yazılı.
6.7/5 Semantics A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
— for an object, causes storage to be reserved for that object;
...
Metod
Metod vücut
bulur.
int g(int lhs, int rhs) {return lhs*rhs;}
Struct
Struct define edilse bile bir değişken yaratılmazsa bellek
ayrılmaz.
struct widget2 // defines structure widget2 that *would* take up ~8 bytes
{
int thing1;
int thing2;
};
Diğer kullanım şekilleri
şöyle
struct widget x; // defines variable x of type struct widget
// which must be defined somewhere prior
struct widget2; // declares structure widget2
// does nothing except tells the compiler there will be some
// structure definition named widget2 coming later
int fn(widget2 *p); // OK widget2 need only be *declared* first
// NO
// struct widget2 y; // illegal - has not be defined yet
struct widget2 // defines structure widget2 that *would* take up ~8 bytes
{
int thing1;
int thing2;
} y; // defines variable y of type struct widget2 that *does* take up ~8 bytes
struct widget2 z[10]; // defines global variable z that *does* take up ~80 bytes
İçinde static alan barındıran struct'lar ise
şöyledir.
struct X { // defines X
static int y; // declares static data member y
};
int X::y = 1; // defines X::y