如何订购C ++类的成员? [英] How should I order the members of a C++ class?

查看:191
本文介绍了如何订购C ++类的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好是拥有所有的私人成员,然后是所有受保护的成员,然后是所有的公共成员?还是相反?或者应该有多个私有,受保护和公共标签,以便操作可以与构造函数等分开吗?在做出此决定时,我应该考虑哪些问题?

Is it better to have all the private members, then all the protected ones, then all the public ones? Or the reverse? Or should there be multiple private, protected and public labels so that the operations can be kept separate from the constructors and so on? What issues should I take into account when making this decision?

推荐答案

我把公共接口放在首位,做这个。我曾经做的事情反向到这,与私人,然后保护,然后公开。回头看,它没有太大的意义。

I put the public interface first, but I didn't always do this. I used to do things backwards to this, with private, then protected, then public. Looking back, it didn't make a lot of sense.

作为一个类的开发者,你很可能熟悉它的内部,但用户该类不太在意,或至少他们不应该。他们最感兴趣的是什么类可以为他们做,对吧?

As a developer of a class, you'll likely be well acquainted with its "innards" but users of the class don't much care, or at least they shouldn't. They're mostly interested in what the class can do for them, right?

所以我把公众放在第一位,并且通常由函数/实用程序组织它。我不希望他们必须通过我的界面去找到所有与X相关的方法,我希望他们以有组织的方式看到所有的东西在一起。

So I put the public first, and organize it typically by function/utility. I don't want them to have to wade through my interface to find all the methods related to X, I want them to see all that stuff together in an organized manner.

我从来不使用多个公共/保护/私人部分 - 太混乱,以我的意见。

I never use multiple public/protected/private sections - too confusing to follow in my opinion.

这篇关于如何订购C ++类的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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