5

I was looking through some open source code and found a class declaration like this:

class Foo{
    private:
        // declarations
    private:
        // declarations
    private:
        // declarations
    public:
        // declarations
};

Is there any time you would want to do such a thing, except to remind you of the members' privacy when having a very long list of declarations?

A Friedrich
  • 593
  • 6
  • 11
  • 7
    I use this occasionally in order to structure members into groups. – cli_hlt Jul 18 '13 at 12:28
  • it seems excessively pedantic, but maybe it wa a remnant of older code that had alternating public and private sections. – TemplateRex Jul 18 '13 at 12:30
  • @TemplateRex No, it was not altering - just like that. – A Friedrich Jul 18 '13 at 12:31
  • There might have been alternating sections in the past that got refactored, there might be a coding convention that specifies what sections must be present ([BDE coding standards](https://github.com/bloomberg/bsl/wiki/Introduction-to-BDE-Coding-Standards) for example)... – David Rodríguez - dribeas Jul 18 '13 at 13:21

4 Answers4

4

This is particularly useful for this type of scenario:

class SomeClass
{
   // COnstructors etc.
   public:
      SomeClass();
      SomeClass(const SomeClass& other);
      ~SomeClass();
      SomeClass operator=(const SomeClass& other);
      SomeClass(const OtherClass& other); 

   // Internal use functions. 
   private:
       int SomePrivateFunc();
       int OtherPrivateFunc();

   // Functions that "do stuff" with this class. 
   public:
       int SomeFunc();
       int OtherFunc();

   // Private member variables. 
   private:
       int x, y; 

   // Public member variables.
   public:
       int value; 
}

(The comments like // Constructurs etc. are just there to show that this is a section of "these things belong together")

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • it's not so useful, a reader can be confused (specially with long portions of code), so some rule is needed, for example `public`-`protected`-`private` order. The comments there are not just for that purpose, but they are the only essential thing ;) – Enoah Netzach Jul 18 '13 at 13:02
  • @EnoahNetzach: That is what coding standards/style guides are for. The first time you might be surprised, but if the code consistently uses the same approach it does not take much to figure it out. We follow this type of approach where I work [BDE coding standards](https://github.com/bloomberg/bsl/wiki/Introduction-to-BDE-Coding-Standards) and the first day it was strange, but then it becomes *normal*. I particularly would have preferred a different order, but the fact that there **is** an order is more important than the choice. – David Rodríguez - dribeas Jul 18 '13 at 13:19
  • Having "dealt with" 2500 line long class declarations, yes, I'm well aware of "it helps to have a strict standard". But you also may end up with several sections of private/public declarations if the class has to be binary compatible with an existing class, as you can't change the order of virtual functions in this case, or things break. – Mats Petersson Jul 18 '13 at 13:21
  • @DavidRodríguez-dribeas, yeah, I know, it's only that continuously switching between accessors is really less clear than commenting why are you grouping functions, or what one particular function does. – Enoah Netzach Jul 18 '13 at 16:05
  • @MatsPetersson, what you are talking about is compiler-dependent -> http://stackoverflow.com/questions/2859761/adding-virtual-function-to-the-end-of-the-class-declaration-avoids-binary-incomp, for example: if you change the class, you have changed it! – Enoah Netzach Jul 18 '13 at 16:05
  • @EnoahNetzach: Yes, but if you have a system built with to ABI for processor X, then you can reasonably rely on the compiler to produce code in the way that the ABI describes. And if you have 10s of millions of people using the actual code you write, and several hundred different organisations building code to work on your systems, you do have to ensure that not every small change means "everyone developing with this has to re-issue all their code" because you add a new virtual function that is optional to use. – Mats Petersson Jul 18 '13 at 16:10
  • @MatsPetersson, generally speaking, adding code not conform to certain defined standards **will** make the _magic_ appear and obscures everything. The thing I was saying is that if you don't want to be strictly processor-dependent, you cannot rely on that. Because it is not written anywhere that changing a class your compiler have to ensure binary compatibility. And that could lead to bigger problems. – Enoah Netzach Jul 18 '13 at 16:22
  • @DavidRodríguez-dribeas sorry, I couldn't edit the previous comment, but I wanted to give you some actual code, a refactoring of yours one: http://coliru.stacked-crooked.com/view?id=a148b29be11fe60a77d4265c9e23cadd-f674c1a6d04c632b71a62362c0ccfc51 – Enoah Netzach Jul 18 '13 at 16:24
  • @EnoahNetzach: Yes, of course, you have to make careful examination of the code generated by the compiler to ensure that you haven't broken things (and have tests that confirm this). I'm just saying that you can't willy nilly change the order of, for example, virtual functions, just because it looks neater. And binary compatibility does matter if you have more than a few "customers" using the code you produce. Breaking that compatibility has costs - but also NOT breaking it has costs, so it's a balance to be struck here. – Mats Petersson Jul 18 '13 at 16:28
  • @EnoahNetzach: I don't understand the point of the link you provided. If you mean that you can potentially reorder code (and rebuild every dependency) yes, you can. Is that better or worse than the original? depends. In our coding style we have have an order for the declaration of the members, and that requires (potentially) opening different public/private sections in the same class. Not necessarily better or worse than your refactor. You can probably google for 'BDE coding standards' – David Rodríguez - dribeas Jul 18 '13 at 17:21
  • @DavidRodríguez-dribeas, I just noticed I made confusion between you and Mats Petersson before :P. However, the code is intended to be written in that way from the beginning. If you modify your code once you have compiled some dependencies, as I was saying, it is not ensured anywhere that adding some methods at the end of the `class` body, binary compatibility is given. Theoretically speaking, ODR is broken whenever you modify your class without rebuilding the dependancies. – Enoah Netzach Jul 18 '13 at 17:35
  • @DavidRodríguez-dribeas, you edited the comment so now I'm just a little confused. The point is that the solution of having functions added at the end of the class body is strictly bound to the compiler, and so on the platform. If you can do so, no problems, it's only a syntax querelle, if not you are not getting the point anyway.. – Enoah Netzach Jul 18 '13 at 17:56
  • @EnoahNetzach: I guess I am not getting the point then. It is undefined behavior to change the class in any way (other than whitespace, comments...) without rebuilding the whole thing. UB means that it might work, it might seem to work or it might not work at all. And still, coding conventions can dictate (as is my case) where the new functions need to be added --which on the premise that it is undefined behavior not to rebuild all dependencies, has no impact – David Rodríguez - dribeas Jul 18 '13 at 18:01
  • Like I said, coding standards may require that funtions are not changed in their order, which is another reason for having a mix of "public/private". It is absolutely the case that if we MOVE virtual functions around in a class, then ALL code using those functions MUST be recompiled. In the case where they are added at the end, you can get away with not doing is, based on the fact that you know the ABI defines the order of virtual functions. It is UB because the standard doesn't require virtual functions to be implemented in any particular way, so if the compiler uses a hash, it may not work. – Mats Petersson Jul 18 '13 at 23:09
1

Yes you can do this to remember members privacy, but also to separate your class's data types, attributes and methods, etc...

jujujuijk
  • 342
  • 3
  • 7
1

It is not wrong, and you could be right, it could be a reminder, semantically is the same as using it only once. In my view (and use) using one section more than once can confuse and mislead readers, not saying that a stronger reminder is using comments, specially for structure members into groups.

Enoah Netzach
  • 747
  • 6
  • 14
  • This answer makes me wonder if there is a C++ IDE that colorize the background of the `public`/`private`/`protected` zone of a `struct`/`class` depending of its access... if not i think that would be nice :) – PaperBirdMaster Jul 18 '13 at 12:45
  • in code editors like _TextMate_ and (if I'm not wrong) _Sublime Text 2_ you can define portions of code with regex and then use those definitions to make highlight rules, so you can define custom syntax regex to recognize `public/private/protected` sections and then assign them a bg colour. – Enoah Netzach Jul 18 '13 at 12:51
  • This sounds good... hope that someday all the OO IDEs offers this by default (not by *regex witchery* ;) – PaperBirdMaster Jul 18 '13 at 12:58
0

I guess you are talking about C++ code, In C# you suppose to declare accessor like (public ) in front of every single variable. it makes the code a bit more readable I guess

Alex G
  • 595
  • 6
  • 21