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

Started by
2 comments, last by Pto 13 years, 4 months ago
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 ?
Advertisement
"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.
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.

"To know the road ahead, ask those coming back."

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.

This topic is closed to new replies.

Advertisement