tags:

views:

722

answers:

6

In C++ sometimes in class definition public members are declared at first and privates later. But the variables or data members are normally private and used by the public methods. So, in this case variables are used but not even declared yet. Thus the code become difficult to understand. But I found renowned programmers, sites or books to declare the private members later. Does anybody knows what is the reason?

+12  A: 

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.

Dominic Rodger
+1: The header file should act as a contract for the users of the class. No implementation belongs there.
kgiannakakis
Except for, of course, inline functions. However, in all but the most trivial cases, those should come after the class's definition.
rlbond
@rlbond - good point.
Dominic Rodger
Except for, templates too and stacks of libraries and so on.. Duplication is non-trivial if you fancy doing it all the time. Just because you can see accessibility does not mean you should tinker with it (there's analog in reflection for managed languages there if you read between the lines)... Good luck with 'contract' thinking in C++..
rama-jka toti
+1  A: 

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.

Jared
+2  A: 

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.

JimDaniel
+3  A: 

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.

kgiannakakis
+5  A: 

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.

A: 

So I have realized why people keep public members at top. Though this is appropriate for classes which will be used by other users, there are some other scenarios. When a class is written as an example, the whole work-process should be clear to the reader. At that time if you are going to put private data members below, it's so troublesome for the readers to keep track. So please, if you are defining a class for example, keep all data members(private or public) at top.