3

In an assignment, I was asked to take a member function from a struct and make it a non-member of that struct, but I don't know what that means, and can't seem to get an answer. I certainly don't want it done for me, so is there any way someone can explain what they're asking of me, so I can figure the rest out?

  • I think this [question](http://stackoverflow.com/questions/15572665/c-structs-with-member-functions-vs-classes-with-public-variables) could be a good starting point – The_Black_Smurf Feb 20 '16 at 22:00
  • 1
    "non-member" means that it is not a member of any struct. It doesn't have any assocation with a particular struct type – M.M Feb 20 '16 at 23:47

1 Answers1

3

A non-member function f that accepts a parameter of type T& named t, e.g. void f(T& t), is very similar to a member function void T::f(), just that *this is replaced by t. The big difference is that it cannot access the private members of the type, but by default that's an advantage because it enhances encapsulation

(Unless it happens to be a friend, of course. From an encapsulation point of view, non-member friend functions are much more similar to member functions than to non-member, non-friend functions.)

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62