简体   繁体   中英

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

I write some code 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

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++.

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). 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. 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++,
except for the public keyword before class
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. It simply isn't canonical 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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