473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Include implementation file in in the header file for template class

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

Oct 25 '06 #1
5 1628
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
--------------------------------------------------------------------------------------

Oct 25 '06 #2
* 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?
Oct 25 '06 #3
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
Oct 25 '06 #4
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.
Oct 25 '06 #5
In article <11*********************@b28g2000cwb.googlegroups. com>,
Kouisawang <KO********@gmail.comwrote:
>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/techt...plates/#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?
Oct 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shen | last post by:
I saw this conclusion in a couple of groups. For example, if you define a class template, in VC++ you have to merge the .h and .cpp files into one file and include it in other files where they have...
28
by: Steven T. Hatton | last post by:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a situation where you had to chose between using...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
7
by: Tomás | last post by:
Let's write a header file which has all sorts of things in it which help a programmer overcome what some people may see as "warts in C++", or maybe things that are just more convenient or nicer to...
8
by: MVM | last post by:
I am new to VS 2003. I did programming in vb6 and vc6, but not through studio. I am having lot of confusion on how to start, where to start? I like to write a program in vc++ under vs 2003. i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.