Best practice: ordering of public/protected/private within the class definition?
The best practice is to be consistent.
Personally, I prefer putting public
methods first, followed by protected
methods, following by private
methods. Member data should in general always be private or protected, unless you have a good reason for it not to be so.
My rationale for putting public
methods at the top is that it defines the interface for your class, so anyone perusing your header file should be able to see this information immediately.
In general, private
and protected
members are less important to most people looking at the header file, unless they are considering modifying the internals of the class. Keeping them "out of the way" ensures this information is maintained only on a need to know basis, one of the more important aspects of encapsulation.
In Clean Code, Robert C. Martin advises coders to always put member variables at the top of the class (constants first, then private members) and methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much. This is a more sensible way to organize code rather than by access modifier.