省略“private”会让人困惑吗?类定义中的关键字?
我最近删除了从类定义中指定的 private
,因为它位于顶部,紧接在 class
关键字之后:
class MyClass
{
private:
int someVariable;
// ...
我认为它是多余的。
一位同事不同意这一点,称这实际上“隐藏”了数据的私有
性质。
我们的大多数遗留代码都明确地声明了访问说明符,并且通常在整个定义中不一致地混合它们。我们的班级规模也往往很大。
我试图使我的新类足够小,以便我的类定义类似于:
class MyClass
{
// 3-4 lines of private variables
protected:
// 3-4 lines of protected functions
public:
// public interface
}
这将允许省略冗余访问说明符,同时(希望)保持 private
成员足够接近 struct
/class
关键字供参考。
我是否为了简洁而牺牲了可读性,或者 struct
/class
关键字就足够了?
I recently removed a private
specified from a class definition because it was at the top, immediately after the class
keyword:
class MyClass
{
private:
int someVariable;
// ...
I thought that it was redundant.
A coworker disagreed with this, saying that it effectively "hid" the private
nature of the data.
Most of our legacy code explicitly states the access specifiers, and usually intermingles them inconsistently throughout the definition. Our classes also tend to be very large.
I'm trying to make my newer classes small enough so that my class definitions are similar to:
class MyClass
{
// 3-4 lines of private variables
protected:
// 3-4 lines of protected functions
public:
// public interface
}
which would allow omission of the redundant access specifier while (hopefully) keeping the private
members close enough to the struct
/class
keyword for reference.
Am I sacrificing readability for brevity, or are the struct
/class
keywords sufficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您非常熟悉所有默认访问级别,那么如果在不需要时忽略它们,您可能不会发现可读性有任何差异。
但是,您会发现许多与您一起工作的人并不是 100% 确定默认访问级别规则。对于经常使用不同语言的人来说尤其如此,因为不同语言的规则可能不同。结果,他们可能会混淆规则。
始终指定访问权限是最安全的选择,哪怕只是为了帮助与您一起工作的人少担心一件事。
If you are very familiar with all the default access levels then you probably won't see any difference in readability if you omit them whenever they are unnecessary.
However you will find that many people you work with aren't 100% sure about the default access level rules. This is especially true for people who regularly use different languages where the rules might be different in the different languages. As a result they might get the rules mixed up.
Always specifying the access is the safest option, if only to help the people you work with have one less thing to worry about.
从技术上讲,类开头的“private”或结构开头的“public”是多余的,但我个人不喜欢混合风格,而是喜欢按访问和声明类型排序。对我来说,可读性比简洁更重要。所以我会有一个“公共方法”、“私有属性”等部分,我将它们格式化为这样:
这当然也会生成冗余的访问声明。另外,我喜欢把“公共”的东西放在第一位,因为这对课程的用户来说是最重要的。所以,无论如何,我在一开始就需要一个访问说明符。我还将“public”放在“struct”的开头。
Technically, "private" at the beginning of a class or "public" at the beginning of a struct is redundant, however I personally do not like the intermingled style but rather like to order by access and by declaration type. Readability is more important to me as brevity. So I would have a section "public methods", "private attributes" and so on and I format them as such:
This of course also generates redundant access declarations. Also, I like putting "public" stuff first because that's most important to users of the class. So, I need an access specifier at the beginning anyway. And I put "public" at the beginning of a "struct" as well.
虽然严格来说是不正确的,但你的同事说得有道理;经验丰富的 C++ 程序员不需要提供给他们的默认访问勺子,但经验不足的程序员可能需要。
更重要的是:我见过和使用过的大多数代码都将公共内容放在第一位,这使得这个问题毫无意义。
While incorrect — strictly speaking — your coworker has a point; an experienced C++ programmer doesn't need the default access spoon fed to them, but a less experienced programmer might.
More to the point: most code I've seen and worked with puts the public stuff first, which renders the question moot.
我个人认为,非常明确通常是一件好事。额外的代码行只是为了增加清晰度而付出的很小的代价。
此外,它还允许您轻松地对成员进行重新排序(如果省略,则 private 必须是第一个,这实际上与您的期望“倒退”)。如果您重新排序,并且有一个
private:
修饰符,其他开发人员就不太可能破坏某些东西。I personally think that being very explicit is generally a good thing. The extra line of code is a small price to pay for the clarity that it adds.
In addition, it allows you to easily reorder your members (private must be first if it's omitted, which is really "backwards" from what you'd expect). If you reorder, and there's a
private:
modifier in place, other developers are less likely to break something.就我个人而言,我认为包含
private
关键字会更清晰,我会保留它。只是为了确保它是私人的并且其他人也知道它。但我认为这是个人品味,每个人都不同。
Personally, I think it is much more clear with the
private
keyword included and I would keep it. Just to make sure it is private and everyone else knows it as well.But I assume this is of personal taste and different for everyone.
我几乎总是从你的类向后安排我的类:首先是公共接口,其次是任何受保护的接口,最后是私有数据。这样我的类的用户就可以简单地查看顶部的公共和受保护接口,而根本不需要查看私有数据。使用该顺序就不可能存在冗余,并且问题变得毫无意义。
如果您更喜欢按照您概述的方式组织您的代码,我相信明确的远远超过一行代码的收益。这样,代码读者就可以完全清楚其意图是什么(例如,如果您将结构更改为类,或者在任何时候将结构更改为类)。
I almost always arrange my classes backwards from yours: Public interface first, any protected interface second, and private data last. This is so that users of my classes can simply look at the public and protected interfaces at the top and need not look at the private data at all. Using that order then there's no possible redundancy and the question become moot.
If you prefer to organize yours in the way you outlined, I believe being explicit far outweighs the gain one of line of code. This way it's completely obvious to code readers what the intention is (for example if you change a struct to a class or the reverse at any point).
我经常首先看到类/结构定义的公共部分,因此受保护/私有的内容稍后出现。
这是有道理的,因为头文件旨在显示类的公共接口实际上是什么。
同时,为什么不使用struct呢?默认情况下是 public...
不过,在代码中特别清楚地了解正在发生的事情总是没有坏处的。这也是为什么我避免使用三元运算符,并且仍然在后面只有一行代码的 if 语句中放入大括号的原因。
然而,最紧迫的问题是你们公司的代码标准是什么?不管你喜欢不喜欢他们,这就是你为公司应该做的标准风格,如果他们说这样做,你就这样做。
I often see the public part of a class/struct definition first, thus the protected/private stuff comes later.
It make sense, as a header file is meant to show what the public interface for your class actually is.
At the same time, why not use struct? public by default...
Still, it never hurts to be extra clear in your code as to what is going on. This is the same reason why I avoid the ternary operator and still put in the braces for an if statement that only has one line of code after it.
The most pressing issue though, is what are your companies code standards? Like them or not, that's the standard style you should do for your company, if they say do it such a way, you do it such a way.