# Function Implementation Inside Class Specification

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is it considered bad style to include short, one-line member function implementation in the specification of a class? Take, for example, the constructor, and active and quit functions in the following:
class GameStack
{
public:
// Constructor
GameStack() : m_active(true) {}

...

bool active() { return m_active; }
void quit()   { m_active = false; }

private:
...

bool m_active;
};



##### Share on other sites
I personally find it more of a matter of taste... the problem with putting the implementation right in line with the definition though is that if (for whatever reason) you want to extend the implementation a bit... then your header fill will get messy...

##### Share on other sites
i think that it is just a matter of preference... I am also pretty sure that it causes the function to be inlined

i usually always do this for my Accessors(Gets) and Mutators(Sets) ....

##### Share on other sites
I tend to put all my simple getters and settings in the header, but try to put anything that is likely to change in the cpp. For example, your quit() function is probably likely to change, and isn't likely to be called often, so I would put it in the cpp file. Probably the same with that constructor as well.

If I know the class is going to be used very often (like a 3D vector class), then I will inline almost all of it.

##### Share on other sites
The draw-back of putting accessors in the header is that you get more compile-time coupling, and thus more re-compiles in response to a change. Also, it does not allow you to export that class member from a DLL, because all users will use the inline version, so future changes will not be binary compatible.

If you link statically and don't mind the physical coupling, then it'll work fine.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634836
• Total Posts
3019544
×