Class design vs. IDE: Are nonmember nonfriend functions really worth it?

后端 未结 7 1055
面向向阳花
面向向阳花 2020-12-21 00:01

In the (otherwise) excellent book C++ Coding Standards, Item 44, titled \"Prefer writing nonmember nonfriend functions\", Sutter and Alexandrescu recommend

7条回答
  •  温柔的废话
    2020-12-21 01:01

    I don't think it would be wrong to say that between them, Sutter, Alexandrescu, and Meyers have done more for the quality of C++ than anybody else.

    One simple question they ask is:

    If a utility function has two independent classes as parameteres, which class should "own" the member function?

    Another issue, is you can only add member functions where the class in question is under your control. Any helper functions that you write for std::string will have to be non-members since you cannot re-open the class definition.

    For both of these examples, your IDE will provide incomplete information, and you will have to use the "old fashion way".

    Given that the most influential C++ experts in the world consider that non-member functions with a class parameter are part of the classes interface, this is more of an issue with your IDE rather than the coding style.

    Your IDE will likely change in a release or two, and you may even be able to get them to add this feature. If you change your coding style to suit todays IDE you may well find that you have bigger problems in the future with unextendable/unmaintainable code.

提交回复
热议问题