What are the reasons for placing member functions in front of member variables or vice versa or vice versa?

Given a class, what reasoning exists for either of the following two styles of code?

Style A:
class Foo {
    private:
        doWork();

        int bar;
}

Style B:
class Foo {
    private:
        int bar;

        doWork();
}

For me it's a tie. I like Style A because member variables feel finer and therefore more general member functions will be displayed. However, I also like style B because the member variables seem to define in OOP style what the class represents.

Are there other things to consider when choosing between these two styles?

+1
source share
7 answers

In C ++, for those who use a class to define in code, in a header file.

Java ( ) , , , JavaDoc. IDE B.

:

  • ++ A, .
  • Java B, .
+2

, , . () - ( ).

B , ( , A). B ( , java, #, php...)

+1

, B , A. , ( , Unreal Script, ), B .

, , . , -.

0

UML, , : , , .

, , .

0

A , , , , .

0

, A, , , - . , , .

class A {
    int bar;
public:
    doWork();
};
0

++, - A, API, . - :

class SomeClass
{
public:
    void foo();
    void bar(int i);

private:
    int m_i;
};

, . - , , , , ().

, , () struct , , . - :

struct SomeMessage
{
    uint64_t m_id;
    int32_t  m_number;
    char     m_name[16];

    explicit SomeMessage(uint64_t id);
};

, - , . , , .

0

Source: https://habr.com/ru/post/1785533/


All Articles