简体   繁体   English

多次使用相同的 C++ 访问说明符

[英]using same C++ access specifiers multiple times

What is the purpose of declaring multiple "public" specifiers over and over again when the next line is just right below it, or few lines below it.当下一行正好在它的正下方或它下方的几行时,一遍又一遍地声明多个“公共”说明符的目的是什么。 I can understand this is requirement when the codes modified the attribute of some identifiers, ie, those that are buried within a macro (hence changing the access attributes within the macro, so we need to "redefined" coming out of the macro), or when we have many identifiers per access specifier section.当代码修改某些标识符的属性时,我可以理解这是要求,即那些隐藏在宏中的标识符(因此更改宏中的访问属性,因此我们需要从宏中“重新定义”),或当每个访问说明符部分有许多标识符时。 But what is the purpose of keep using "public", "public", over and over again?但是,一遍又一遍地使用“public”、“public”的目的是什么?

Code ...代码 ...

class CDrawMFCView : public CView
{
protected: // create from serialization only
    CDrawMFCView();
    DECLARE_DYNCREATE(CDrawMFCView)

// Attributes
public:
    CDrawMFCDoc* GetDocument() const;

// Operations
public:

// Overrides
public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
  // etc.,

};

For starters, it's not necessary for how the code is NOW, it's necessary because the code sections may:对于初学者来说,现在不需要代码如何,这是必要的,因为代码部分可能:

  • become a lot longer变得更长
  • be cut-and-pasted into a different order, or even into a different class, or copied into a new class被剪切并粘贴到不同的顺序,甚至到不同的类中,或复制到新的类中
  • have some sections change the access specifier without the previous or following ones changing让某些部分更改访问说明符,而不更改前一个或后继的访问说明符

If you relied the section having the same access specification as the previous section, vry very often you (or you, six months from now, or someone else) would forget to change it when the code changed, and then the code would be wrong.如果您依赖与上一部分具有相同访问规范的部分,则您(或您,六个月后,或其他人)经常会在代码更改时忘记更改它,然后代码就会出错。

It could be useful when looking at a class with more methods than lines on your screen, so you just look at, say当查看一个类的方法多于屏幕上的行时,它可能很有用,所以你只需看看,说

...
void f();
void g();
void h();
...

By repeating public: a few times you could remind people that all these are public (of course, having more methods than lines in your terminal either means your terminal is a bit small or the class is too big).通过重复public:几次你可以提醒人们所有这些都是公开的(当然,在你的终端中有比行更多的方法要么意味着你的终端有点小或类太大了)。

There is no language purpose to doing that.这样做没有语言目的。 I think it is bad style.我认为这是不好的风格。 But some people like to divide up everything of a particular kind into a section, and then divide the section into public/protected/private areas.但是有些人喜欢将特定类型的所有东西划分为一个部分,然后将该部分划分为公共/保护区/私人区域。 Then when they don't happen to have anything but public elements, then the public keyword gets repeated redundantly.然后,当它们碰巧除了 public 元素之外没有任何东西时, public 关键字就会重复重复。

I think its dumb.我认为它很愚蠢。 But some people find it useful to organize their code this way.但是有些人发现以这种方式组织他们的代码很有用。

There is only one formal reason: data members between access specifiers are ordered sequentially in memory, but data members across access specifiers may be reordered in memory.只有一个正式的原因:访问说明符之间的数据成员在内存中按顺序排列,但跨访问说明符的数据成员可能在内存中重新排序。

class Foo {
  public:
    int a;
    int b; // Must come after a
  public:
    int c; // Does not have to come after a and b.
};

The second public: gives more room for the optimizer.第二个public:给优化器更多的空间。

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

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