忽略“私有”是很困惑的。关键字从类定义? [英] Is it confusing to omit the "private" keyword from a class definition?

查看:171
本文介绍了忽略“私有”是很困惑的。关键字从类定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近删除了从类定义中指定的 private ,因为它位于顶部,紧接在关键字:

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;
// ...

我认为这是多余的。

一位同事不同意这一点,说它有效地隐藏了数据的 private 性质。

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
}

这将允许省略冗余访问说明符,同时(希望)保持 private 成员足够接近 struct / class 关键字以供参考。

which would allow omission of the redundant access specifier while (hopefully) keeping the private members close enough to the struct/class keyword for reference.

我为了简洁而牺牲了可读性, $ c> struct / class 关键字足够了?

Am I sacrificing readability for brevity, or are the struct/class keywords sufficient?

推荐答案

如果您非常熟悉所有默认访问级别,那么如果您在不必要时忽略它们,那么您可能不会看到任何可读性差异。

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.

但是,您会发现许多与您合作的人都不能100%确定默认访问级别规则。这对于经常使用不同语言的人来说尤其如此,其中规则在不同语言中可能不同。

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.

这篇关于忽略“私有”是很困惑的。关键字从类定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆