简体   繁体   English

在C ++中每个类成员之前是可接受的用法访问说明符

[英]Is acceptable usage access specifiers before each member of class in C++

I write some code c++ 我写了一些代码c ++

public class SomeClass 
{
private: 
   int m_CurrentStatus;
   int m_PreviouseStatus;
public:
   int get_CurrentStatus() { return m_CurrentStatus; }
   int get_PreviouseStatus() { return m_PreviouseStatus; }
}

in c# style 在c#风格

public class SomeClass 
{
private:  int m_CurrentStatus;
private:  int m_PreviouseStatus;
public:   int get_CurrentStatus() { return m_CurrentStatus; }
public:   int get_PreviouseStatus() { return m_PreviouseStatus; }
}

Such usage access specifiers before each member of class is acceptable? 在每个成员之前这样的用法访问说明符是可以接受的吗? Or can trouble compiler spend more time for compilation or other effects? 或者麻烦编译器花费更多时间进行编译或其他效果? Code successfully compiled with no warning. 代码编译成功,没有警告。

What you describe is legal C++. 你所描述的是合法的C ++。

The impact on compilation times will depend on the compiler. 对编译时间的影响取决于编译器。 However, practically, you will probably be hard pressed to detect any difference of compilation times. 但实际上,您可能很难发现编译时间的任何差异。

There may be either impact or benefit on code readability - ie ability of a human to understand what is going on. 对代码可读性可能有影响或有益 - 即人类理解正在发生的事情的能力。 Generally, people prefer "sections" (eg the declaration of several members following a single access modifier ( public , private , or protected )) quite well, as long as the sections don't get too large (eg fill more than a screen when editing the code). 通常,人们更喜欢“部分”(例如,单个访问修饰符( publicprivateprotected )之后的几个成员的声明),只要这些部分不会变得太大(例如,填充超过屏幕时)编辑代码)。 So doing it the way you are may be unpopular with other developers. 因此,按照您的方式进行操作可能不受其他开发人员的欢迎。 This is highly subjective though - different people will have different preferences. 这是非常主观的 - 不同的人会有不同的偏好。 However, if you find other people objecting to your approach, listen to them - unless you are happy to be unpopular with other team members, lose jobs, etc etc. 但是,如果您发现其他人反对您的方法,请倾听他们 - 除非您乐意与其他团队成员不相处,失去工作等等。

There may or may not be an impact on layout of data members in the class. 对班级中的数据成员的布局可能有影响,也可能没有影响。 Different versions of the C++ standard make different guarantees but there are considerable freedoms for compilers to lay out classes differently. 不同版本的C ++标准提供了不同的保证,但编译器有相当大的自由来布置不同的类。 If you are writing code that relies on (or tests for) particular class layout (order of data members, offsets, etc) you might observe a difference. 如果您编写的代码依赖于(或测试)特定的类布局(数据成员的顺序,偏移等),您可能会发现差异。 Or you might not. 或者你可能不会。 However, these things are permitted to vary between implementations (compilers) anyway so writing code that relies on specific layouts is often a bad idea anyway. 但是,无论如何,允许这些内容在实现(编译器)之间变化,因此编写依赖于特定布局的代码通常是一个坏主意。

It's legal syntax and shouldn't upset the compiler, you can have as many public and private blocks as you want. 它是合法的语法,不应该扰乱编译器,你可以拥有任意数量的公共和私有块。 I don't see that it's an improvement syntactically though. 我不认为它在语法上是一种改进。

It's perfectly legal syntax for C++, 这是C ++完全合法的语法,
except for the public keyword before class 除了class前的public关键字
and the missing semicolon ( ; ) after the closing curly bracket of class. 并且在类的右大括号之后缺少分号( ; )。

BUT I've never seen something like that, I suppose it's surely not commonly used and, in my opinion, it adds nothing to code readability (on contrary, I personally find it more cumbersome to read). 但是我从来没有见过这样的东西,我想它肯定不常用,而且在我看来,它没有增加代码的可读性(相反,我个人认为阅读起来比较麻烦)。

It's certainly legal and free from compile time penalties to put access specifiers before each and every class member. 它肯定是合法的,并且在每个类成员之前放置访问说明符都没有编译时间的惩罚。 You could also put 50 semicolons at the end of every line. 你也可以在每一行的末尾加上50个分号。 It simply isn't canonical C++. 它根本不是规范的C ++。

It is Legal and also very correct. 它是合法的,也是非常正确的。 You won't use an object oriented language if you want to have your members public. 如果您希望公开您的成员,则不会使用面向对象的语言。 A basic usage of object oriented programming is information hiding. 面向对象编程的基本用法是信息隐藏。

The only thing that can trouble compiler in your case(in a matter of time), is that your function block is inside the class. 在你的情况下(在时间问题上)唯一可以使编译器麻烦的是你的功能块在类中。

What do I mean? 我的意思是什么?

If you have a main.cpp and a class.h , and your functions are declared and defined inside class.h, your compilation will take a little longer, rather than if your function body was in functions.cpp 如果你有一个main.cpp和一个class.h,你的函数在class.h中声明和定义,你的编译需要更长的时间,而不是你的函数体是在functions.cpp中。

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

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