Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several "private" declarations in class

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?

like image 588
A Friedrich Avatar asked Jul 18 '13 12:07

A Friedrich


3 Answers

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")

like image 199
Mats Petersson Avatar answered Oct 19 '22 12:10

Mats Petersson


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

like image 20
jujujuijk Avatar answered Oct 19 '22 12:10

jujujuijk


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.

like image 1
Enoah Netzach Avatar answered Oct 19 '22 11:10

Enoah Netzach