non-member-functions

Static member functions

…衆ロ難τιáo~ 提交于 2019-11-27 01:26:46
问题 After reading sbi and Eli Bendersky's answers in this question I started to wondering what static member functions are for. A class' friend free function shouldn't be able to do anything a static member function can do? If so, why/when should I prefer a static member function to a friend free one? 回答1: In general: Require access to private members static member functions have access to private members of the class. If you need that, you can use a static member function. You have to declare it

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

折月煮酒 提交于 2019-11-26 18:41:56
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 non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don't they? So, first question, should not they be members then? Reading a bit further, he considers the STL functions and indeed some functions which are not implemented

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

a 夏天 提交于 2019-11-26 06:32:12
问题 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 non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don\'t they? So, first question, should not they be members then?

Operator overloading : member function vs. non-member function?

空扰寡人 提交于 2019-11-25 22:44:28
问题 I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand, overloaded operator declared as a friend is symmetric because we pass two arguments of the same type and hence, they can be compared. My question is that when i can still compare a pointer\'s lvalue to a reference, why are friends preferred? (using an