delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/26/23:51:20

Message-ID: <20000627035110.87660.qmail@hotmail.com>
X-Originating-IP: [208.160.246.197]
From: "Nimrod Abing" <n_abing AT hotmail DOT com>
To: djgpp AT delorie DOT com
Subject: Re: Pointers and Arrays (Newbie)
Date: Tue, 27 Jun 2000 11:51:10 PHT
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

>From: "Adrian Smith" <Adrian AT atpelectronics DOT connectfree DOT co DOT uk>
>Reply-To: djgpp AT delorie DOT com
>To: djgpp AT delorie DOT com
>Subject: Re: Pointers and Arrays (Newbie)
>Date: Mon, 26 Jun 2000 09:38:36 +0100
>
>Thanks Simon,
>                             I appreciate what you've said about 
>declerations
>and defenitions, but why does it allow you to define the contents of a
>character array element in the header file, if all defenitions should be in
>cpp-files and declerations in header files. ??

Look at it in the context of the ``#include <header.h>''. This statement
is a compiler directive. It tells the compiler to ``include the file
header.h located in one of the include directories'' in the compilation
process. IOW, the header file becomes part of the file that you are
compiling. The compiler then takes this ``composite'' file as a single
compilation unit. All symbols (vars and funcs) in the header file
become part of the file that includes it.

       +--------+
       |header.h|<-----------------+
       +--------+                  |
                                   |
       +--------+                  |
       |file.cpp|----- includes ---+
       +--------+

       +--------+
       |header.h|---------+
       +--------+         +----> compiles as one file
       |file.cpp|---------+
       +--------+

If you were to include a header file that contains function and variable
definitions with other files you would get compile errors. BTW, the file
``header.h'' could very well be ``header.c'' or ``header.cc'' it doesn't
matter to the compiler when you use it in an #include directive. the .h
extension is just a convention. (It also helps some editors like emacs
determine the file type and set the appropriate editing mode)

>Simon Sulser <simonsulser AT hotmail DOT com> wrote in message
>news:8ivoqt$imj$1 AT news1 DOT sunrise DOT ch...
> > You should only put declarations in header files and all definitions in
> > cpp-files. Maybe you included your header several times in different cpp
> > files.
> >
> > -- header file --
> >
> > // decleration
> > extern char const * message;
> >
> > -- cpp file --
> >
> > #include "header.h"
> >
> > // definition
> > char const *message = "Blablablabla";
> >
> > Of course if you only need message in one cpp file you don't have to 
>write
> > the declaration into a header file at all. You can even declare it as
> > static, which means it's local to this cpp file.
> >
> >
>
>


----------------
_nimrod_a_abing_


------------------------------------------
Homepage: http://www.geocities.com/n_abing

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019