简体   繁体   English

C ++类访问说明符详细程度

[英]C++ Class Access Specifier Verbosity

A "traditional" C++ class (just some random declarations) might resemble the following: “传统的”C ++类(只是一些随机声明)可能类似于以下内容:

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: 看一下Java / C#风格的相同代码片段,我们得到:

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. 我在使用未与通常的“* .hpp / * .inl”对分开的仅标题模板代码时发现这一点尤其正确。 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. 我的问题很简单,源于我从未见过其他人在他们的C ++代码中主动执行此操作的事实。

Assuming that I don't have a "Class View" capable IDE, are there any obvious drawbacks to using this level of verbosity? 假设我没有“Class View”能力的IDE,那么使用这种级别的冗长有什么明显的缺点吗?

Any other style recommendations are welcome! 欢迎任何其他风格的建议!

"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. 我花了很多时间使用Java,就像分别为每个字段和方法指定访问说明符的样式一样。 However when I am programming in C++, I always use the style shown in your first code snippet. 但是,当我使用C ++编程时,我总是使用第一个代码片段中显示的样式。

Personally, I find it very annoying to have to specify the access qualifier for every symbol. 就个人而言,我发现必须为每个符号指定访问限定符非常烦人。 It makes things harder to read, not easier, and encourages the very bad habit of freely mixing private and public stuff throughout the class definition. 它使事情变得更难阅读,而不是更容易,并鼓励在整个班级定义中自由混合私人和公共事物的坏习惯。 I see this kind of mess all the time. 我总是看到这种混乱。 In C#, I try to mitigate this with #region private , etc, which hopefully encourages future maintainers to keep things clean. 在C#中,我尝试使用#region private等来缓解这种情况,这有望鼓励未来的维护者保持清洁。

There's nothing wrong with it, though you will raise eyebrows. 虽然你会引起人们的注意,但它没有任何问题。 The primary advantage of keeping the access specifiers separate is it encourages placing all the private members and methods at the top of the class -- together. 保持访问说明符分离的主要优点是它鼓励将所有私有成员和方法放在类的顶部 - 一起。

If your class is too big to fit on one screenfull than it probably should be either broken up into more than one class, and/or any implicitly inline functions should be explicitly declared inline with the implementation moved out of the class. 如果你的类太大而不适合放在一个screenfull上,那么它应该分解成多个类,和/或任何隐式内联函数都应该内联显式声明,并将实现移出类。

The disadvantage to the latter approach is that is rarely done, and it is not a good idea to surprise other developers in this way, and it requires the access modifier to be typed a million times. 后一种方法的缺点是很少进行,并且以这种方式让其他开发人员感到惊讶并不是一个好主意,并且它需要输入访问修饰符一百万次。 Using the traditional approach will save having to needlessly type the modifier over and over and is also the expected approach. 使用传统方法可以省去不必要地反复输入修改器,也是预期的方法。

Indeed, semantically it makes no difference but you will do yourself and your colleagues a great favour if you'll just follow what's accepted. 事实上,在语义上它没有任何区别,但如果你只是按照接受的方式,你会做自己和你的同事。

Personally I like my class like so: 我个人喜欢我的课程:

struct S {
    S(int m) : m(m) { }
    S(const S& o);
    S& operator=(const S& o);

    void f() const;
    std::string g();

private:
    void help();

private:
    int m;
};

But I will change my manners without thinking twice if I commit into a repository that isn't strictly mine, because I know how appreciative I'd be if someone would commit their code to my repositories following my customs. 但是如果我进入一个并非严格意义上的存储库,我会不加思索地改变我的举止,因为我知道如果有人将我们的代码提交到我的存储库之后,我会有多么感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM