Include implementation file in in the header file for template class

Discussion in 'C++' started by Kouisawang, Oct 25, 2006.

  1. Kouisawang

    Kouisawang Guest

    Hello all,
    I have a question when I found the following code;
    --------------------------------------------------------------------------------------
    #ifndef FOO_H
    #define FOO_H
    //some more include

    class foo{
    //member functions, variables
    }

    #include "foo.cc" /* this line that I would like to ask that what is it
    for? why we have to include the implementation file? Doesn't is violate
    the encapsulation or information hiding concept? */

    #endif
    --------------------------------------------------------------------------------------
    I found it in my C++ book and the author doesn't say anything about it.

    Thanks in advance.
    -O.kittipot
     
    Kouisawang, Oct 25, 2006
    #1
    1. Advertising

  2. Kouisawang

    Kouisawang Guest

    Opps I forget one line :p
    --------------------------------------------------------------------------------------
    #ifndef FOO_H
    #define FOO_H
    //some more include

    template <class myType> //<--------add this line
    class foo{
    //member functions, variables (some are of myType)
    }

    #include "foo.cc" /* this line that I would like to ask that what is it
    for? why we have to include the implementation file? Doesn't is violate
    the encapsulation or information hiding concept? */

    #endif
    --------------------------------------------------------------------------------------
     
    Kouisawang, Oct 25, 2006
    #2
    1. Advertising

  3. Re: Include implementation file in in the header file for templateclass

    * Kouisawang:
    > Opps I forget one line :p
    > --------------------------------------------------------------------------------------
    > #ifndef FOO_H
    > #define FOO_H
    > //some more include
    >
    > template <class myType> //<--------add this line
    > class foo{
    > //member functions, variables (some are of myType)
    > }
    >
    > #include "foo.cc" /* this line that I would like to ask that what is it
    > for? why we have to include the implementation file? Doesn't is violate
    > the encapsulation or information hiding concept? */
    >
    > #endif
    > --------------------------------------------------------------------------------------


    Unless the compiler implements the 'export' keyword, which only Comeau
    does (not counting an earlier version of Borland, nor undocumented
    option for Intel) it needs the template definition of something where
    that something is used.

    See the FAQ item titled "Why can't I separate the definition of my
    templates class from it's (sic) declaration and put it inside a .cpp
    file?", currently at <url:
    http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.

    Hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 25, 2006
    #3
  4. Kouisawang

    Marco Wahl Guest

    > Hello all,
    > I have a question when I found the following code;
    > --------------------------------------------------------------------------------------
    > #ifndef FOO_H
    > #define FOO_H
    > //some more include
    >
    > class foo{
    > //member functions, variables
    > }
    >
    > #include "foo.cc" /* this line that I would like to ask that what is it
    > for? why we have to include the implementation file?


    Doing this you can e.g. pull out
    inline-function-definitions from the header-file
    leaving just this line '#include "foo.cc"' in the
    header. This may increase the clarity of the header.

    > Doesn't is violate
    > the encapsulation or information hiding concept? */
    >
    > #endif
    > --------------------------------------------------------------------------------------
    > I found it in my C++ book and the author doesn't say anything about it.
    >
    > Thanks in advance.
    > -O.kittipot
     
    Marco Wahl, Oct 25, 2006
    #4
  5. Kouisawang

    Pete Becker Guest

    Re: Include implementation file in in the header file for templateclass

    Kouisawang wrote:
    > Hello all,
    > I have a question when I found the following code;
    > --------------------------------------------------------------------------------------
    > #ifndef FOO_H
    > #define FOO_H
    > //some more include
    >
    > class foo{
    > //member functions, variables
    > }
    >
    > #include "foo.cc" /* this line that I would like to ask that what is it
    > for? why we have to include the implementation file? Doesn't is violate
    > the encapsulation or information hiding concept? */
    >


    It puts the definitions of the class's member function in the header.
    Most compilers handle templates that way: the compiler has to see those
    definitions wherever they're used. The linker removes duplicates.

    That's separate from information hiding. Information hiding isn't about
    concealing source code. It's about not making internal details available
    to code that doesn't need them. Private is still private, so access to
    the class's internals hasn't been changed.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Oct 25, 2006
    #5
  6. Kouisawang

    Greg Comeau Guest

    In article <>,
    Kouisawang <> wrote:
    >Hello all,
    >I have a question when I found the following code;
    >--------------------------------------------------------------------------------------
    >#ifndef FOO_H
    >#define FOO_H
    >//some more include
    >
    >class foo{
    >//member functions, variables
    >}
    >
    >#include "foo.cc" /* this line that I would like to ask that what is it
    >for? why we have to include the implementation file? Doesn't is violate
    >the encapsulation or information hiding concept? */
    >
    >#endif
    >--------------------------------------------------------------------------------------
    >I found it in my C++ book and the author doesn't say anything about it.


    Assuming this is what you are referring to although your example
    does not use a template:

    http://www.comeaucomputing.com/techtalk/templates/#export
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 25, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aguilar, James
    Replies:
    2
    Views:
    707
    Aguilar, James
    Jul 16, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    960
    Andreas Bogenberger
    Feb 22, 2008
  4. Replies:
    0
    Views:
    329
  5. A L
    Replies:
    1
    Views:
    516
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page