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;
开发者_JS百科 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 it doesn't suggest the private functions.
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
}
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.
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.).
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
精彩评论