C++ include files

krazygluon
Unregistered
 
Post: #1
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
Quote this message in a reply
Member
Posts: 111
Joined: 2002.06
Post: #2
The class definition goes into the header file. The code for the member functions goes into the .cpp file.

Mark Szymczyk
http://www.meandmark.com
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #3
this is typed off the top of my head, but try somthing like:

MyObject.h
Code:
#ifndef MyObject_H
#define MyObject_H

//forward declarations
class MyOtherObject;

class MyObject
{
//methods
public:
  MyObject();
  DoSomething();
  ~MyObject();
//vars
int mynumber;
MyOtherObject * myObj2;
};
#endif // MyObject

MyObject.c
Code:
#include "MyObject.h"
#include "MyOtherObject.h"

MyObject::MyObject ()
{
//initialize variables here
mynumber=0;
myObj2= new MyOtherObject();
}

void MyObject::DoSomething ()
{
//do something!
mynumber++;
myObj2->DoSomethingImportant();
}

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

If I missed something, someone here will correct me!
Quote this message in a reply
Member
Posts: 40
Joined: 2004.12
Post: #4
Looks fine save for the line:
Code:
#endif // MyObject
You can't put comments on preprocessor directives.

Jericho
Quote this message in a reply
omgomghilol
Unregistered
 
Post: #5
All template code (definitions and declarations) must also be in header files.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #6
seven Wrote:Looks fine save for the line:
Code:
#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.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2004.10
Post: #7
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)
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2004.10
Post: #8
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
Quote this message in a reply
Member
Posts: 40
Joined: 2004.12
Post: #9
Sorry, but I don't know what is confusing.

Jericho
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
You *can* put comments on preprocessor directives -- GCC, MSVC and CW all accept it, even if it's not allowed by the standard.
Quote this message in a reply
Member
Posts: 78
Joined: 2004.06
Post: #11
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
**/

Thanks

When in doubt ... read the Read Me
10.5.6 | MacBook Pro 2.5x2 | 4 GB RAM | GeForce 8600M GT
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
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).
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Member
Posts: 78
Joined: 2004.06
Post: #14
Thanks Smile I guess that makes sense!

When in doubt ... read the Read Me
10.5.6 | MacBook Pro 2.5x2 | 4 GB RAM | GeForce 8600M GT
Quote this message in a reply
Member
Posts: 78
Joined: 2004.06
Post: #15
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?

When in doubt ... read the Read Me
10.5.6 | MacBook Pro 2.5x2 | 4 GB RAM | GeForce 8600M GT
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Include in Xcode Atomical 19 6,813 Jul 8, 2005 04:56 PM
Last Post: Atomical