Class Order Style

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 21 15:06:23 PST 2017


On Tuesday, February 21, 2017 22:41:40 Lenny Lowood via Digitalmars-d-learn 
wrote:
> On Monday, 20 February 2017 at 18:02:20 UTC, Jolly James wrote:
> > On Monday, 20 February 2017 at 13:50:26 UTC, ketmar wrote:
> >> just add ddoc documentation to 'em, and then it doesn't matter
> >> in which order they are declared: people will generate
> >> documentation to find out how to use your code. ;-)
> >
> > ah okay, thx
> >
> >
> > But what about this?
> >
> >>>class A
> >>>{
> >>>
> >>>private:
> >>>    int a;
> >>>    int b;
> >>>
> >>>public:
> >>>    int c;
> >>>    int d;
> >>>
> >>>}
> >>>
> >> or
> >>
> >>>class A
> >>>{
> >>>
> >>>    private
> >>>    {
> >>>
> >>>        int a;
> >>>        int b;
> >>>
> >>>    }
> >>>    public
> >>>    {
> >>>
> >>>        int c;
> >>>        int d;
> >>>
> >>>    }
> >>>
> >>>}
>
> Me as a beginner would like to know this, too ...

It's completely a stylistic preference. There are a number of different ways
to order your member variables and functions, and there are several
different ways to apply attributes to them.

As far as public and private go, I think that most folks either use the
labels like

private:
public:

or they put them directly on the members like

private int a;

I suspect that the folks who programmed a lot in C++ tend to do the former,
since that's the way you have to do it in C++, and I'd guess that the folks
who have programmed a lot in Java or C# typically do the latter, because
that's how you have to do it in those languages.

There is no right or wrong way. Personally, I use the labels like in C++ and
put the public stuff first in a class or struct and the private stuff last
(and I think that that's what Phobos mostly does), but you'll find plenty of
folks who do it differently.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list