Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why defining private members below public members in C++?

Tags:

c++

private

I do things that way round since users of my class don't care about the private members, they're interested in the public API (i.e. how to use my class).

Also, in header files I'm generally just declaring member functions, rather than defining them, so I'm not accessing any private members anyway.


We read text from top to bottom, so the most relevant information should be at the top. In a class definition, that's the public interface.


Private members and implementation should be hidden from the header file. Putting the private member definitions at the bottom is a quick way to do so. Perhaps it is better to use the Pimpl idiom and hide the private part of your class in an internal struct.


Normally private members don't matter. If I'm looking at a class to determine how to use it somewhere else in code I don't care about it's internals so put private members at the bottom since i don't need to know about them. If I"m modifying a class then I'll take the time to find the private members and know they'll be at the bottom instead of having to skim the entire class.


We are like opposites: My Question

My reasoning is that when you are becoming familiar with a class it is more beneficial to first get introduced to the public interface then go deeper into the private members when you need to. If you start by looking at the private members, you have no context how they are used.