Re: #include question

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 03/01/04


Date: Mon, 1 Mar 2004 10:59:00 +0000

In message <ljv0c.18979$6K.11191@nwrddc02.gnilink.net>, Dan Moos
<dan.moos@verizon.net> writes
>No that is what I meant, and you answered my question.
>
>Lets see if I got this right...
>
>The definition of a class or template tells the compiler what to do when an
>object is created. After the compiler has created all the objects of a
>certain class, the class definition is no longer useful, SO it doesn't end
>up in the final executable, thus, you really aren't adding redundant code to
>the executable, in fact you aren't even adding the initial definition. The
>definition is for the COMPILER, not the executable.
>
>Am I on the right track?
>
>
Yes, but not quite at the destination.

Definitions of variables and functions tell the compiler what to do when
the name they declare is used.

Definitions of classes and structs tell the compiler enough about
storage requirements + the available functionality so that it your
source code can use objects of the relevant type.

However the class/struct also usually requires an implementation. That
consists of the definitions of the functions that were declared in the
definition of the class/struct.

C++ provides an option for some (or even all) the implementation to be
provided within the class definition and requires that there shall be
some latter 'magic' that handles possible redefinition errors when more
than a single TU has opted to provide actual callable versions rather
than inline the code.

Summary:

By default a variable is defined (which includes declaration) unless it
is part of a class definition where it will be declared and it is the
job of the ctor to define it. Other variables can only be declared by
use of the 'extern' keyword that effectively switches off the default
behaviour.

A function is usually declared in a header file. Its implementation is
called a definition and is usually placed in a separate .cpp file. If
the linker sees the results of more than one definition of a function
then it will issue a redefinition error unless:

a) the function was declared as inline either implicitly because the
definition was in a class definition or explicitly by use of the inline
keyword.

b) The multiple definitions occur in library files being used to resolve
external definitions. In this case the first definition fund will be
used and the problem will be treated as solved. Note this can result in
some very subtle errors but in practice such problems are rare.

A class/struct can be declared (i.e. only the name is given) which is
enough for use of references and pointers to that type. It is normally
defined (in a header file) and, for use it need to be implemented
(normally in a separate TU)

enums can only de simultaneously defined and declared as C++ does not
provide a mechanism for pure declaration of an enum (there are good
reasons but let us not go there today)

Typedefs are effectively pure declarations because they introduce new
names for existing (possibly compound) types.

I think that covers most issues until we start looking at generic
facilities provided via templates.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: #include
    ... connects to the this header file and the respective function is ... this is often used to separate declaration from implementation. ... compile the .c file using the -c switch, or you can build a library. ... mex-file) and use #include to give the compiler the function's ...
    (comp.soft-sys.matlab)
  • Re: #include question
    ... >>The definition of a class or template tells the compiler what to do when ... After the compiler has created all the objects of a ... > By default a variable is defined (which includes declaration) unless it ... > A function is usually declared in a header file. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: circular dependencies, unrecognized base types, oh-my
    ... >> declaration of C in B.h and of B in C.h? ... > You can't sensibly control which header file ... > the compiler reaches first. ... The preprocessor will run "#ifndef B_H" and determine that it's undefined. ...
    (comp.lang.cpp)
  • Re: Making function private/ not supplying declaration in header file
    ... > than a declaration is not necessary. ... I believe if the compiler can ... > for all functions in a header file. ... used outside a compilation unit in a header file. ...
    (comp.lang.c)
  • Re: circular dependencies, unrecognized base types, oh-my
    ... > should I put a forward declaration of C in B.h and of B in C.h? ... You can't sensibly control which header file the compiler reaches first. ... I've added void C::functo ...
    (comp.lang.cpp)