Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 403
Konstantin Samsonov Avatar asked Jan 26 '17 12:01

Konstantin Samsonov


People also ask

Which access specifier can be used for member functions of a class?

Which access specifier is usually used for data members of a class? Explanation: All the data members should be made private to ensure the highest security of data. In special cases we can use public or protected access, but it is advised to keep the data members private always.

What are the access specifiers in C?

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

Which access specifier should be used?

If the members have to be accessed from anywhere in the program and other packages also, which access specifier should be used? Explanation: The access specifier must be public so as to access the members outside the class and anywhere within the program without using inheritance.

Which of the following access specifier is used as a default in a class definition?

Explanation: By definition if a class has no access specifiers, it defaults to private accessibility.


1 Answers

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 - i.e. ability of a human to understand what is going on. Generally, people prefer "sections" (e.g. 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 (e.g. 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.

like image 89
Peter Avatar answered Sep 29 '22 12:09

Peter