6

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.

Colin
  • 3,394
  • 1
  • 21
  • 29
  • Possible duplicate of [Can there be two public section in a class? If yes then why ? And in which circumstances we do so?](http://stackoverflow.com/questions/2719641/can-there-be-two-public-section-in-a-class-if-yes-then-why-and-in-which-circu) – Mohsen_Fatemi Jan 26 '17 at 12:39
  • 3
    It's acceptable in the sense that it conforms to the language specification. It's unacceptable in the sense that nobody's going to want to work with you if you do it. – molbdnilo Jan 26 '17 at 12:43
  • @molbdnilo, I double it. There're C++ code styles and people are used to them. What you did is like using camelcase in python scripts. – c4pQ Jan 26 '17 at 13:12

5 Answers5

4

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.

Peter
  • 35,646
  • 4
  • 32
  • 74
1

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.

Colin
  • 3,394
  • 1
  • 21
  • 29
  • I spend 99% in c# but some code write in c++ just used c# notation and nothing more – Konstantin Samsonov Jan 26 '17 at 13:02
  • @KonstantinSamsonov If you really do spend most of your time in C#, it may be best not to try to make your C++ code look the same. That may help you avoid incorrect assumptions in cases where the two languages differ. – Josh Sanford Jan 26 '17 at 13:32
  • Ok I allready undestand my mistake, I start insert 'public:' in my c# code So i decide that need some visual difference from different project parts – Konstantin Samsonov Jan 31 '17 at 05:25
1

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

roalz
  • 2,699
  • 3
  • 25
  • 42
1

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

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

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

Theo.Fanis
  • 444
  • 6
  • 15