How Non-Member Functions Improve Encapsulation

后端 未结 8 880
心在旅途
心在旅途 2020-11-29 08:47

I read Scott Meyers\' article on the subject and quite confused about what he is talking about. I have 3 questions here.

Question 1

To expl

相关标签:
8条回答
  • 2020-11-29 09:16

    He specifically says "non-member non-friend functions" (emphasis mine). If you would need to make the non-member function a fiend his algorithms says it should be a member function unless it's operator>> or operator<< or needs type conversions on its left-most argument.

    0 讨论(0)
  • 2020-11-29 09:19

    Of the four cases he provides for making functions non-members, the closest that your proposed vector methods would come to is this one:

    else if (f can be implemented via C's
             public interface)
       make f a non-member function;
    

    But you can't implement methods like push_back, insert or operator[] via a public interface. Those are the public interface. It might be possible to implement push_back in terms of insert, but to to a large degree, what public interface are you going to be using for such methods?

    Further the cases for giving friendship to non-member functions are really special cases as I see it, operator<< and operator>>, and type conversions, would both require very accurate, and unfiltered data from the class. These methods are naturally very invasive.

    While I'm not a fan of Dr. Dobbs, or any of the claimed "C++ gurus", I think in this case you might be double guessing your own implementation. Scott Meyer's algorithm seems reasonable to me.

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