Re: #include question

From: Dan Moos (dan.moos_at_verizon.net)
Date: 03/01/04


Date: Mon, 01 Mar 2004 20:16:43 GMT


"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:mc6QziE0dxQAFwOs@robinton.demon.co.uk...
> 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.

right on, that really helps.

Thanks againg folks



Relevant Pages

  • Re: friend class in template
    ... This template takes 3 type parameters, the first of which is named ... The friend declaration attempts to declare the class ... the friend declaration doesn't do what the author apparently ... thought it would do (at least not on a conforming compiler). ...
    (microsoft.public.vc.language)
  • 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: 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: HELP: Visual Studio.NET 2003 building woes .... :S
    ... I get a lot of repeated errors of the above 3 plus lots of template errors. ... I am having major problems trying to port/build my application under Visual Studio.NET 2003 using the unmanaged/native C++ compiler. ... But if the CString conversion is public, the declaration does not need to be inside the class. ...
    (microsoft.public.vc.mfc)
  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)