How should I order the members of a C++ class?

后端 未结 15 1748
Happy的楠姐
Happy的楠姐 2020-12-04 11:50

Is it better to have all the private members, then all the protected ones, then all the public ones? Or the reverse? Or should there be multiple private, protected and pub

相关标签:
15条回答
  • 2020-12-04 12:37

    In practice, it rarely matters. It's primarily a matter of personal preference.

    It's very popular to put public methods first, ostensibly so that users of the class will be able to find them more easily. But headers should never be your primary source of documentation, so basing "best practices" around the idea that users will be looking at your headers seems to miss the mark for me.

    It's more likely for people to be in your headers if they're modifying the class, in which case they should care about the private interface.

    Whichever you choose, make your headers clean and easy to read. Being able to easily find whatever info I happen to be looking for, whether I'm a user of the class or a maintainer of the class, is the most important thing.

    0 讨论(0)
  • 2020-12-04 12:42

    I usually define first the interface (to be read), that is public, then protected, then private stuff. Now, in many cases I go a step forward and (if I can handle it) use the PIMPL pattern, fully hiding all the private stuff from the interface of the real class.

    class Example1 {
    public:
       void publicOperation();
    private:
       void privateOperation1_();
       void privateOperation2_();
    
       Type1 data1_;
       Type2 data2_;
    };
    // example 2 header:
    class Example2 {
       class Impl;
    public:
       void publicOperation();
    private:
       std::auto_ptr<Example2Impl> impl_;
    };
    // example2 cpp:
    class Example2::Impl
    {
    public:
       void privateOperation1();
       void privateOperation2();
    private: // or public if Example2 needs access, or private + friendship:
       Type1 data1_;
       Type2 data2_;
    };
    

    You can notice that I postfix private (and also protected) members with an underscore. The PIMPL version has an internal class for which the outside world does not even see the operations. This keeps the class interface completely clean: only real interface is exposed. No need to argue about order.

    There is an associated cost during the class construction as a dynamically allocated object must be built. Also this works really well for classes that are not meant to be extended, but has some short comings with hierarchies. Protected methods must be part of the external class, so you cannot really push them into the internal class.

    0 讨论(0)
  • 2020-12-04 12:42

    Note that (depending on your compiler and dynamic linker), you can retain compatibility with previous versions of a shared library by only adding to the end of the class (i.e. to the end of the interface), and not removing or changing anything else. (This is true for G++ and libtool, and the three part versioning scheme for GNU/Linux shared libraries reflects this.)

    There's also the idea that you should order members of the class to avoid wasted space due to memory alignment; one strategy is to order members from smallest to largest size. I've never done this either in C++ or C though.

    0 讨论(0)
提交回复
热议问题