Large scale usage of Meyer's advice to prefer Non-member,non-friend functions?

后端 未结 8 1079
無奈伤痛
無奈伤痛 2021-02-05 00:15

For some time I\'ve been designing my class interfaces to be minimal, preferring namespace-wrapped non-member functions over member functions. Essentially following Scott Meyer

8条回答
  •  日久生厌
    2021-02-05 01:02

    As stated in the article, STL has both member and non-member functions. This isn't because of his preference -- it's mostly because many of the free functions operate on iterators and iterators are not in a class hierarchy (because STL wants pointers on arrays to be first-class iterators).

    I strongly disagree with this article for C++, because the inconsistency would be annoying, and in modern IDE's intellisense would be broken.

    In C#, however, with extension methods, this is all very good advice. There, you get the best of both worlds -- you can make non-member functions that can appear to be member functions. They also can be in separate files -- getting all of the benefits of this practice without the inconsistency drawback.

提交回复
热议问题