Should you pass member variables within member functions?

Sort of a style question here. Say I have a class A which has to do a sequence of reasonably complex things to its member variable B b

class A {
 public:
  void DoStuffOnB(){
   DoThing1();
   DoThing2();
   DoThing3();
  }
 private:
  B b;
  void DoThing1(){ /* modify b */ }
  void DoThing2(){ /* modify b */ }
  void DoThing3(){ /* modify b */ }
};

where the DoThings functions only depend on b (or other member variables and some passed parameters). If I want to make those functions re-usable in the future outside of that class, I'm better off writing them as:

class A {
 public:
  void DoStuffOnB(){
   DoThing1(b);
   DoThing2(b);
   DoThing3(b);
  }
 private:
  B b;
  void DoThing1(B& b){ /* modify b */ }
  void DoThing2(B& b){ /* modify b */ }
  void DoThing3(B& b){ /* modify b */ }
};

and then my DoThing functions can just be copied elsewhere in the future. Am I better off writing the function to take all relevant parameters like that, or should the function only take non-member parameters?

In case the answer is "you should write the function to take all relevant parameters", why would one bother to put it in a class?

When should you use a free function, and when should you use a member function?


Assuming from the context that the "do something on B" functions only operate on the B member and not other state in A then:

  • If the functions directly manipulate/operate on the private state of B then they should be members of B .
  • Else they should be free functions.

  • A member function is a member function because its' scope has access to the member variables without having to use referencing and pointer syntax. As someone mentioned earlier this would most likely make it simpler to code and maintain so you would use this method unless you needed the function to be a free function that might take the same type data but from different classes in which case you would have to pass by reference or use pointers to gain access to the scope of the variable.


    Should you pass member variables within member functions?

    There is no need to pass member variables to member functions, since the member functions have access to all the data members.

    It's similar to free standing functions accessing static file local variables. The functions have access to the statically declared variables in the same translation unit.

    When should you use a freestanding function and when should you use a member function?

    In general, use a member function when the functionality is associated with the object.

    Use a freestanding function when

  • the class has static members

  • or functionality is associated with a class and doesn't use static members.

  • You can also use freestanding functions when the same functionality can apply to different objects.
    For example, let's talk serialization or outputting of an object.

    One can define a method, load_from_buffer() in an object, but it won't work with POD types.

    However, if a function load_from_buffer() is made freestanding, it can be overloaded for different types, such as int , char , double and with templates, an overload can be made to call objects derived from an interface.

    Summary
    Prefer to use member methods when they require access to data members of an object. Use static member methods when they access static data members or there is a need for the functionality without an instance of an object (think encapsulation). Freestanding functions also provide the capability of functionality to different objects based on function overloading.

    There are no hard rules, just use what you think will be easiest to maintain, assist in correctness and robustness and speed up development.

    Just to confuse people, here is an article by Scott Meyers:
    How Non-Member functions increase encapsulation

    Remember, in order for a free standing function to access data members of an object, the data members must be given public access or the function needs to be a friend of the object. The classic example is overloading the stream operators for a class.

    链接地址: http://www.djcxy.com/p/80814.html

    上一篇: Groovy SSL签名验证:签名长度不正确?

    下一篇: 你是否应该在成员函数中传递成员变量?