Jump to content
  • Advertisement
Sign in to follow this  
Phineas

Function Implementation Inside Class Specification

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!