Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Class Access Specifier Verbosity

A "traditional" C++ class (just some random declarations) might resemble the following:

class Foo
{
public:
  Foo();
  explicit Foo(const std::string&);
  ~Foo();

  enum FooState
  {
    Idle, Busy, Unknown
  };

  FooState GetState() const;
  bool GetBar() const;
  void SetBaz(int);

private:
  struct FooPartialImpl;

  void HelperFunction1();
  void HelperFunction2();
  void HelperFunction3();

  FooPartialImpl* m_impl; // smart ptr
  FooState m_state;
  bool m_bar;
  int m_baz;
};

I always found this type of access level specification ugly and difficult to follow if the original programmer didn't organize his "access regions" neatly.


Taking a look at the same snippet in a Java/C# style, we get:

class Foo
{
  public: Foo();
  public: explicit Foo(const std::string&);
  public: ~Foo();

  public: enum FooState
  {
    Idle, Busy, Unknown
  };

  public: FooState GetState() const;
  public: bool GetBar() const;
  public: void SetBaz(int);

  private: struct FooPartialImpl;

  private: void HelperFunction1();
  private: void HelperFunction2();
  private: void HelperFunction3();

  private: FooPartialImpl* m_impl; // smart ptr
  private: FooState m_state;
  private: bool m_bar;
  private: int m_baz;
};

In my opinion, this is much easier to read in a header because the access specifier is right next to the target, and not a bunch of lines away. I found this especially true when working with header-only template code that wasn't separated into the usual "*.hpp/*.inl" pair. In that scenario, the size of the function implementations overpowered this small but important information.


My question is simple and stems from the fact that I've never seen anyone else actively do this in their C++ code.

Assuming that I don't have a "Class View" capable IDE, are there any obvious drawbacks to using this level of verbosity?

Any other style recommendations are welcome!

like image 464
PolyTex Avatar asked Apr 11 '10 05:04

PolyTex


People also ask

What are the C access specifiers?

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is the default access specifier in C++ when no access specifier is declared?

If we do not mention any access specifier for the members inside the class, then by default, the Access Specifier in C++ for the members will be Private. All functions are accessible to the friend function of the class.

Which is the default access specifier in the class of C++?

If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private . If the derived class is declared with the keyword struct , the default access specifier in its base list specifiers is public .

Why is private a default access level of class in C++?

- The default access level assigned to members of a class is private. - Private members of a class are accessible only within the class and by friends of the class. - Protected members are accessible by the class itself and its sub- classes. - Public members of a class can be accessed by anyone.


1 Answers

"When in Rome, do as the Romans do."

I, having spent a lot of time with Java, like the style of specifying access specifiers for every field and method separately. However when I am programming in C++, I always use the style shown in your first code snippet.

like image 50
missingfaktor Avatar answered Oct 24 '22 00:10

missingfaktor