Sign in to follow this  
thedodgeruk

best layout for header files? and auto rearrange of cpp file?

Recommended Posts

what is the best order to arrange the header file:

private:
protected:
public:

inside of each would it be
variables then funtions??



and also once you have ordered your header file , is there a tool that can reorder your cpp to the same order as the funtions order in the header file..

want to clean up my code as much as possible ?

Share this post


Link to post
Share on other sites
"Best"? It is purely subjective.

I order from public to private, because public is the most interesting 99% of the time you're going to read the code. Private is least interesting, as it should only need to be read when you're fixing bugs.

Functions ahead of variables, for much the same reasons. My variables are almost always private anyway, so they end up at the end of the class file anyway.

Protected should be avoided if possible, it can cause encapsulation issues.

Share this post


Link to post
Share on other sites
Very interesting.

Before I read this I didn't realise protected variables should be avoided. I've had a look around t'internet and it seems that most people say they shouldn't ever really be used. It does make sense though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Monkan
I've had a look around t'internet and it seems that most people say they shouldn't ever really be used. It does make sense though.


I've been told to never use protected data, and instead to always use private data with protected access functions. Is that a good rule?

I use protected quite regularly and never had an issue, but sometimes private has saved me weeks worth of pain when people I didn't know were using the code. Its all about knowing when to use it. Like const, you can't put it on every variable on every function, it should be used where appropriate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this