A Friedrich
A Friedrich

Reputation: 603

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?

Upvotes: 6

Views: 389

Answers (4)

Mats Petersson
Mats Petersson

Reputation: 129374

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

Upvotes: 4

Enoah Netzach
Enoah Netzach

Reputation: 777

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.

Upvotes: 1

Alex G
Alex G

Reputation: 595

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

Upvotes: 0

jujujuijk
jujujuijk

Reputation: 342

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

Upvotes: 1

Related Questions