Effective C++ Item 23 Prefer non-member non-friend functions to member functions

后端 未结 7 1878
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 03:09

While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer no

相关标签:
7条回答
  • 2020-11-27 03:35

    I think the reason for this rule is that by using member functions you may rely too much on the internals of a class by accident. Changing the state of a class is not a problem. The real problem is the amount of code you need to change if you modify some private property inside your class. Keeping the interface of the class (public methods) as small as possible reduces both the amount of work you will need to do in such a case and the risk of doing something weird with your private data, leaving you with an instance in an inconsistent state.

    AtoMerZ is also right, non-member non-friend functions can be templated and reused for other types as well.

    By the way you should buy your copy of Effective C++, it's a great book, but do not try to always comply with every item of this book. Object Oriented Design both good practices (from books, etc.) AND experience (I think it's also written in Effective C++ somewhere).

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