newbie's question: declaration versus definition

O

orca

Hi there

I would like to know exactly what is the distinction between a
declaration and a definition, with some simple but illuminating
examples for variables and functions, which I am somewhat more used
to; as I guess, some examples with more than one file (or translation
unit) might be expedient.

According to Stroustrup's "Programming Principles and Practice with C+
+", pages 51 and 61, a declaration is a "program statement specifying
how a piece of code can be used", whereas a definition is a "statement
that introduces a new name into a program and sets aside memory for a
variable". Thus, if I understand correctly, a declaration does not
assign memory to a named object, right? Also, I have read somewhere
that a definition is also always a declaration; is that correct??

Examples:

int num;
string name="John Doe";

are definitions.

int func(string, bool);
external double Pi;

are declarations; they do not (why??) set aside memory???

Thanks for any guidance
 
D

David Pratt

orca said:
Hi there

I would like to know exactly what is the distinction between a
declaration and a definition, with some simple but illuminating
examples for variables and functions, which I am somewhat more used
to; as I guess, some examples with more than one file (or translation
unit) might be expedient.

According to Stroustrup's "Programming Principles and Practice with C+
+", pages 51 and 61, a declaration is a "program statement specifying
how a piece of code can be used", whereas a definition is a "statement
that introduces a new name into a program and sets aside memory for a
variable". Thus, if I understand correctly, a declaration does not
assign memory to a named object, right? Also, I have read somewhere
that a definition is also always a declaration; is that correct??

Examples:

int num;
string name="John Doe";

are definitions.

int func(string, bool);
external double Pi;

are declarations; they do not (why??) set aside memory???

Thanks for any guidance

looking at functions as the best example, You declare the function in your
header file (usually) like so:

int foo(int bar);

this sets up how this function is to be called.

now in you cpp file you have the definition:

int foo(int bar)
{
return bar;
}

The short and sweet, the way I remember it is the declaration lets the
rest of the visible scope know that there is a such a thing as foo as well
as the rules of its use, and the definition "implements" the declaration.
There are situations where your declaration and definition are one and the
same.
 
S

Saeed Amrollahi

Hi there

I would like to know exactly what is the distinction between a
declaration and a definition, with some simple but illuminating
examples for variables and functions, which I am somewhat more used
to; as I guess, some examples with more than one file (or translation
unit) might be expedient.

According to Stroustrup's "Programming Principles and Practice with C+
+", pages 51 and 61, a declaration is a "program statement specifying
how a piece of code can be used", whereas a definition is a "statement
that introduces a new name into a program and sets aside memory for a
variable". Thus, if I understand correctly, a declaration does not
assign memory to a named object, right? Also, I have read somewhere
that a definition is also always a declaration; is that correct??

Examples:

int num;
string name="John Doe";

are definitions.

int func(string, bool);
external double Pi;

are declarations; they do not (why??) set aside memory???

Thanks for any guidance

Hi

I think your understanding is good and of course your reference is
excellent.
According to C++ committee draft (section 3.1 and chapter 7):
Declaration introduces a new name into a region or translation unit.
Declaration specifies how names are to be interpreted.
A declaration is a definition, if in addition to name introduction,
it contains some memory allocation or in case of functions
the function body will be added:
Most of declaration are definitions too. For example, if you write
int i; // declaration and definiton
first, a new name is introduced and second part of memory for i
is allocated (by compiler or operating system).
but in the following statement:
extern int i; // just declaration
just a name introduced. you say to compiler, the definition of
i is somewhere else. and of course with above statement no memory is
allocated.
Also in function case
int sqr(int); // just declaration
and
int sqr(int x) // declaration and defintion
{
return x * x;
}
You can have more than one declaration for a name
but you have one and only one definition for a name.

I hope it helps.
only one definition

P.S. If you have access to The C++ Programming Language by Bjarne
Stroustrup
see section 4.9.
 
J

James Kanze

I would like to know exactly what is the distinction between a
declaration and a definition, with some simple but
illuminating examples for variables and functions, which I am
somewhat more used to; as I guess, some examples with more
than one file (or translation unit) might be expedient.

Basically:

-- A definition is a declaration, always. But not all
declarations are definitions.

-- There should be exactly one definition for each object in
the program. There are exceptions to this for templates and
inline functions, but you shouldn't be using either until
you've at least a little experience with the simpler things.

-- It is the definition which creates the object, all other
declarations only indicate to the compiler that it exists.

-- On the other hand, a compiler "knows" about all objects for
which there is a visible declaration in scope, regardless of
whether that declaration is a definition or not.

What holds for objects holds also for functions and references.
Types have somewhat different rules.

The real problem is that the rules determining whether a
declaration is a definition or not are some complex, and not in
the least orthogonal. Hysterical (or should that be historical)
reasons.
According to Stroustrup's "Programming Principles and Practice
with C+ +", pages 51 and 61, a declaration is a "program
statement specifying how a piece of code can be used", whereas
a definition is a "statement that introduces a new name into a
program and sets aside memory for a variable". Thus, if I
understand correctly, a declaration does not assign memory to
a named object, right?

More or less. It doesn't initialize the object, either. All it
does is tell the compiler that the object exists, somewhere.
Also, I have read somewhere that a definition is also always a
declaration; is that correct??
Yes.


int num;
string name="John Doe";
are definitions.
int func(string, bool);
external double Pi;
are declarations; they do not (why??) set aside memory???

Because they aren't definitions:). Basically:

-- for variables and references, any declaration that doesn't
have the keyword extern or has an initializer is a
definition, and

-- for functions, any declaration which defines the function
body is an definition.

Thus, for objects:
int a ;
int a = 42 ;
extern int a = 42 ;
are all definitions,
extern int a ;
is not a definition. (But as usual, there are exceptions. In a
class definition:
static int const a = 42 ;
is still not a definition.)
 
O

orca

Basically:

 -- A definition is a declaration, always.  But not all
    declarations are definitions.

 -- There should be exactly one definition for each object in
    the program.  There are exceptions to this for templates and
    inline functions, but you shouldn't be using either until
    you've at least a little experience with the simpler things.

 -- It is the definition which creates the object, all other
    declarations only indicate to the compiler that it exists.

 -- On the other hand, a compiler "knows" about all objects for
    which there is a visible declaration in scope, regardless of
    whether that declaration is a definition or not.

What holds for objects holds also for functions and references.
Types have somewhat different rules.

The real problem is that the rules determining whether a
declaration is a definition or not are some complex, and not in
the least orthogonal.  Hysterical (or should that be historical)
reasons.


More or less.  It doesn't initialize the object, either.  All it
does is tell the compiler that the object exists, somewhere.


Because they aren't definitions:).  Basically:

 -- for variables and references, any declaration that doesn't
    have the keyword extern or has an initializer is a
    definition, and

 -- for functions, any declaration which defines the function
    body is an definition.

Thus, for objects:
           int             a ;
           int             a = 42 ;
    extern int             a = 42 ;
are all definitions,
    extern int             a ;
is not a definition.  (But as usual, there are exceptions.  In a
class definition:
    static int const       a = 42 ;
is still not a definition.)

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thank you all for the explanations; I see I will still have to dig
some more;
C++ definitely is a very subtle language...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top