iDevGames Forums
C++ include files - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: C++ include files (/thread-4265.html)

Pages: 1 2

C++ include files - krazygluon - May 18, 2006 08:43 AM

So I'm writing some classes for the game I'm working on and am packaging my classes into header files.

My question is this: what of a class definition, goes into the .h file and what goes into an associated .cpp file? at the moment it works for me to just dump the whole class (member functions, prototype and all) into the .h file, but that seems like it defeats the intended purpose of a header file.

The Gluon

C++ include files - szymczyk - May 18, 2006 11:33 AM

The class definition goes into the header file. The code for the member functions goes into the .cpp file.

C++ include files - codemattic - May 18, 2006 12:49 PM

this is typed off the top of my head, but try somthing like:

#ifndef MyObject_H
#define MyObject_H

//forward declarations
class MyOtherObject;

class MyObject
int mynumber;
MyOtherObject * myObj2;
#endif // MyObject

#include "MyObject.h"
#include "MyOtherObject.h"

MyObject::MyObject ()
//initialize variables here
myObj2= new MyOtherObject();

void MyObject::DoSomething ()
//do something!

MyObject::~MyObject ()
//do any cleanup and free any variables here    
delete myObj2;

If I missed something, someone here will correct me!

C++ include files - seven - May 18, 2006 02:15 PM

Looks fine save for the line:
#endif // MyObject
You can't put comments on preprocessor directives.

C++ include files - omgomghilol - May 19, 2006 11:09 AM

All template code (definitions and declarations) must also be in header files.

C++ include files - DoG - May 19, 2006 12:38 PM

seven Wrote:Looks fine save for the line:
#endif // MyObject
You can't put comments on preprocessor directives.
And you can't go around confusing people, or take it up with gcc on this one.

C++ include files - jfaller - May 19, 2006 12:41 PM

The comments above are mostly right, but leave off a few corner cases. I'll be the pedantic a--hole, and fill in the details.

Code in the header file should include the declaration for the class, and any inline member functions. Although some compilers can optimize calls across object file boundaries, it's an optimization pass during/after the link phase. In general, I put all 2-5 line member functions in the .h file (think setters and accessors, and simple functions). All else goes in the .cpp file.

This leaves off one other corner case in C++, namely template classes. When dealing with templated classes, most compilers require you to put the definition in the .h file as well. The reason is because of when templates are instantiated during typical C++ compilation process.

The latest full standard of C++ includes support for the "export" (I think) keyword that eliminates the need to have template definitions in .h files, although not all compilers support it. I don't know the present state of g++ on the mac, so somone with more intimiate knowledge should chime in. (Additionally, using the export keyword causes an additional phase in the linker, so links could be slow.)

In general, here are my rules.

- Templated classes/functions -- all code goes in the header (mostly for portability between compilers, you know)
- All short member functions go in header
- All else in the .cpp file.

One other note. Per the latest C/C++ standards, I think you can put // comments on preprocessor lines. I think in old C compilers (think before C-9X) you couldn't use // comments on these lines because the ANSI preprocessor didn't recognize // comments at all. I don't have my copy of the standards here, but if the preprocessor won't accept // lines, and the standard allows it, I'd fill a bug report with the gcc team.

(small edit for typo)

C++ include files - jfaller - May 19, 2006 12:43 PM

DoG Wrote:And you can't go around confusing people, or take it up with gcc on this one.

I assume this means, you beat me to my comment! Smile

C++ include files - seven - May 20, 2006 09:32 AM

Sorry, but I don't know what is confusing.

C++ include files - OneSadCookie - May 20, 2006 03:39 PM

You *can* put comments on preprocessor directives -- GCC, MSVC and CW all accept it, even if it's not allowed by the standard.

C++ include files - Shunter - Apr 22, 2007 07:45 PM

Ok, another question on a somewhat similar topic. I have non-OO code in C++ that I want to split up into multiple source files because it's too much for my brain in one long file.

I've put some processing functions (definitions) into a .cpp file, and function prototypes, structure definitions, and standard library includes into a .h file. I've included the .h file from the main, and included the .cpp (function definitions) from the .h file.

Xcode doesn't like this. It seems to make senese to me, but it looks like it's trying to compile the .cpp file by itself instead of including it in the others. (Yes, I'm somewhat new to c++). Any suggestions?

This is a simple hierarchy. main.cpp->process.h->process.cpp
All with #inlcude


C++ include files - OneSadCookie - Apr 22, 2007 08:59 PM

put function prototypes, type declarations and constant definitions in .h files. Remember to use header guards or #pragma once. Put function definitions in .cpp files. Never #include a .cpp file. #include the .h file that contains prototypes, type declarations and constant definitions before you use them (whether that use is in a .h or a .cpp).

C++ include files - PowerMacX - Apr 23, 2007 06:04 AM

C++ include files - Shunter - Apr 23, 2007 06:43 AM

Thanks Smile I guess that makes sense!

C++ include files - Shunter - Apr 23, 2007 09:42 AM

Ok, well I got it all formatted correctly now, but I'm consistantly getting the error " error: 'string' does not name a type". I tried including <string> but even that didn't help. Sad I'm assuming you don't include the .cpp file becuase the compiler automatically seletcs it as a compile target if it's part of the project?