Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I define the body for a private function?

Tags:

c++

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 it doesn't suggest the private functions.

like image 999
Martijn Courteaux Avatar asked Jun 18 '10 13:06

Martijn Courteaux


People also ask

How I define private function?

More Definitions of Private function Private function means any gathering of persons for the purpose of deliberation, education, instruction, entertainment, amusement, or dining that is not intended to be open to the public and for which membership or specific invitation is a prerequisite to entry.

How do you define a private function in CPP?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

How do you define a private function in JavaScript?

In a JavaScript class, to declare something as “private,” which can be a method, property, or getter and setter, you have to prefix its name with the hash character “#”.

How do you call a private member function?

You can't call private member functions from main. Your public member functions can call their private functions though. Show activity on this post. I am writing a c++ program where i need to call a private member function from main function.


2 Answers

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
}
like image 113
wheaties Avatar answered Nov 11 '22 19:11

wheaties


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.

like image 45
Artelius Avatar answered Nov 11 '22 18:11

Artelius