Include implementation file in in the header file for template class

K

Kouisawang

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
 
K

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
--------------------------------------------------------------------------------------
 
A

Alf P. Steinbach

* 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
 
M

Marco Wahl

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.
 
P

Pete Becker

Kouisawang said:
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.
 
G

Greg Comeau

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

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

http://www.comeaucomputing.com/techtalk/templates/#export
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top