Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include 1

Status
Not open for further replies.

gray

Programmer
Nov 12, 1999
1
0
0
GB
when putting classes into seperate files, do you put the functions in the same file as the class defination or into another header file.
 
Hi gray!<br>
<br>
I put all the implentations of my class functions into the same .cpp file. I suppose you could split them up, but it might make it harder to find them later when you're maintaining your app.<br>
<br>
I don't know if you've seen this trick, but in the header file, you surround it with precompiler statements so that it doesn't get included twice. I also include all the standard (lowercase) include files in there, surrounded by precompiler statements as well. This ensures that if I include the header in another .cpp file, and headers that are needed are included too.<br>
<br>
example:<br>
//---My.cpp<br>
#define __MY_DEF__<br>
#include &quot;my.h&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp; {class implementation}<br>
<br>
<br>
//---My.h<br>
#ifndef __MY_H__<br>
#define __MY_H__<br>
<br>
// Only include these if they haven't been before<br>
#ifndef _IOSTREAM_<br>
#include &lt;iostream&gt;<br>
#endif<br>
<br>
#ifndef _DEQUE_<br>
#include &lt;deque&gt;<br>
#endif<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; {define my class here}<br>
<br>
#ifdef __MY_DEF__<br>
&nbsp;&nbsp;&nbsp;&nbsp; {variables, etc. whose scope should be limited to my.cpp}<br>
#else<br>
&nbsp;&nbsp;&nbsp;&nbsp; {variables, etc. whose scope should be public}<br>
#endif<br>
#endif<br>
<br>
<br>
You can also use the #PRAGMA ONCE to prevent double-includes, but I think it's specific to Microsoft's VC++.<br>
<br>
Chip H.<br>

 
Best to put definitions into a .cpp. The class should be a general declaration of what variables and functions are available. The .cpp defines those variables and functions as to specifically how they will be used. Keeps it clearer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top