views:

130

answers:

4

Hi,

I have a header like this (header guards not shown):

class GameSystem
{
public:
    GameSystem(Game *pcGame);
    virtual ~GameSystem();
    void Setup();
private:
    void InitGame();
    void RunGame();
    void ExitGame();
    Game *m_pcGame;

    /* Properties */
    int m_nWidth;
    int m_nHeight;
    int m_nFps;
    bool m_bFullscreen;
};

Where can I define the body for InitGame(), RunGame() and ExitGame()? Can I define it in my .cpp file? If so, how? Or am I obliged to make their body in my .h file?

I'm using Eclipse and I began typing: void GameSystem:: and then he doesn't suggest the private functions.

Solved

I was only confused by Eclipse. It didn't suggest my to override the private functions. But is just OK doing it. No problem.
Sorry, for this non-question-worth question
Thanks all!

+5  A: 

Yes, you can define then in a .cpp file. Just put #include "MyHeader.h" at the beginning of the file. You'll also need to start each function like so

void GameSystem::Init(){
     //stuff
}
wheaties
@wheaties: But `Setup()` isn't the problem. How to define the private functions?
Martijn Courteaux
same way, really. Just include the header file.
wheaties
@Martijn Courteaux: Exactly the same way as with `Setup()`
John Dibling
Ok, thanks. I was confused by Eclipse his suggestion. It works perfect. I though: "Eclipse doesn't suggest it, so it would be wrong"
Martijn Courteaux
+9  A: 

Generally you would define both public and private functions in the .cpp file.

One reason to define functions in the .h file is if you want them to be inlineable.

Artelius
+2  A: 

I think you are concerned about private functions should be private with the meaning of "not visible in the header (which is the interface)". But private means "not accessible from outside the class", i.e. only functions of the class can call private functions. If you don't want (human) users of your class see these implementation details, you need to use a suitable design patterns (facade pattern e.g.).

ur
A: 

Defining in .h means inline, but defining in .cpp and using forward declaration, you can make your compilation more efficient.

See here:http://en.wikipedia.org/wiki/Forward_declaration

rhapsodyn