When should I prefer non-member non-friend functions to member functions?

后端 未结 4 800
南方客
南方客 2020-12-08 03:27

Meyers mentioned in his book Effective C++ that in certain scenarios non-member non-friend functions are better encapsulated than member functions.

Example:

相关标签:
4条回答
  • 2020-12-08 03:41

    Non-member functions are commonly used when the developer of a library wants to write binary operators that can be overloaded on either argument with a class type, since if you make them a member of the class you can only overload on the second argument (the first is implicitly an object of that class). The various arithmetic operators for complex are perhaps the definitive example for this.

    In the example you cite, the motivation is of another kind: use the least coupled design that still allows you to do the job.

    This means that while clearEverything could (and, to be frank, would be quite likely to) be made a member, we don't make it one because it does not technically have to be. This buys you two things:

    1. You don't have to accept the responsibility of having a clearEverything method in your public interface (once you ship with one, you 're married to it for life).
    2. The number of functions with access to the private members of the class is one lower, hence any changes in the future will be easier to perform and less likely to cause bugs.

    That said, in this particular example I feel that the concept is being taken too far, and for such an "innocent" function I 'd gravitate towards making it a member. But the concept is sound, and in "real world" scenarios where things are not so simple it would make much more sense.

    0 讨论(0)
  • 2020-12-08 03:44

    More generally, what are the rules of when to use which?

    Here is what Scott Meyer's rules are (source):

    Scott has an interesting article in print which advocates that non-member non-friend functions improve encapsulation for classes. He uses the following algorithm to determine where a function f gets placed:

    if (f needs to be virtual)
        make f a member function of C;
    else if (f is operator>> or operator<<)
    {
       make f a non-member function;
       if (f needs access to non-public members of C)
          make f a friend of C;
    }
    else if (f needs type conversions on its left-most argument)
    {
       make f a non-member function;
       if (f needs access to non-public members of C)
          make f a friend of C;
    }
    else if (f can be implemented via C's public interface)
       make f a non-member function;
    else
       make f a member function of C;
    

    His definition of encapsulation involves the number of functions which are impacted when private data members are changed.

    Which pretty much sums it all up, and it is quite reasonable as well, in my opinion.

    0 讨论(0)
  • 2020-12-08 03:47

    Locality and allowing the class to provide 'enough' features while maintaining encapsulation are some things to consider.

    If WebBrowser is reused in many places, the dependencies/clients may define multiple convenience functions. This keeps your classes (WebBrowser) lightweight and easy to manage.

    The inverse would be that the WebBrowser ends up pleasing all clients, and just becomes some monolithic beast that is difficult to change.

    Do you find the class is lacking functionality once it has been put to use in multiple scenarios? Do patterns emerge in your convenience functions? It's best (IMO) to defer formally extending the class's interface until patterns emerge and there is a good reason to add this functionality. A minimal class is easier to maintain, but you don't want redundant implementations all over the place because that pushes the maintenance burden onto your clients.

    If your convenience functions are complex to implement, or there is a common case which can improve performance significantly (e.g. to empty a thread safe collection with one lock, rather than one element at a time with a lock each time), then you may also want to consider that case.

    There will also be cases where you realize something is genuinely missing from the WebBrowser as you use it.

    0 讨论(0)
  • 2020-12-08 04:02

    I often choose to build utility methods outside of my classes when they are application specific.

    The application is usually in a different context then the engines doing the work underneath. If we take you example of a web browser, the 3 clear methods belongs to the web engine as this is needed functionality that would be difficult to implement anywhere else, however, the ClearEverything() is definitely more application specific. In this instance your application might have a small dialog that has a clear all button to help the user be more efficient. Maybe this is not something another application re-using your web browser engine would want to do and therefor having it in the engine class would just be more clutter.

    Another example is a in a mathematic libraries. Often it make sense to have more advanced functionality like mean value or standard derivation implemented as part of a mathematical class. However, if you have an application specific way to calculate some type of mean that is not the standard version, it should probably be outside of your class and part of a utility library specific to you application.

    I have never been a big fan of strong hardcoded rules to implement things in one way or another, it’s often a matter of ideology and principles.

    M.

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