Should you pass member variables as part of member functions?

The style question type is here. Say I have a class A that needs to do a sequence of fairly 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 depend only on b (or other member variables and some parameters passed). If I want these functions to be reused in the future outside of this class, I better write 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 simply be copied elsewhere in the future. Is it better for me to write a function to accept all the relevant parameters, for example, or if the function accepts only non-member parameters?

In the case of the answer “you must write a function to accept all the relevant parameters”, why not put it in a class?

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

+7
c ++ design oop reusability
source share
3 answers

Assuming from the context that the functions "do something on B" act only on member B , and not in a different state in A , then:

  • If functions directly manipulate / work in private state B , then they must be members of B
  • Otherwise, they should be free functions.
+3
source share

A member function is a member function because its "scope" has access to member variables without the need for references and pointer syntax. As mentioned earlier, this would most likely simplify coding and support, so you would use this method if you didn't need a function to be a free function that can receive data of the same type, but from different classes, in this case you will have to follow the link or use pointers to access the scope of the variable.

+1
source share

Should you pass member variables in member functions?

There is no need to pass member variables to member functions, because member functions have access to all data elements.

It is similar to free functions accessing static local variables of a file. Functions have access to statically declared variables in the same translation unit.

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

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

Use the standalone function when

  • class has static elements

  • or functionality associated with the class and does not use static members.

You can also use autonomous functions when the same functionality can be applied to different objects.
For example, let’s talk about serializing or outputting an object.

You can define the load_from_buffer() method in the load_from_buffer() , but it will not work with POD types.

However, if the load_from_buffer() function is executed separately, it can be overloaded for different types, such as int , char , double and with templates, overloading can be done to call objects received from the interface.

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

There are no hard and fast rules, just use what you think will be the easiest way to support, help in the correctness and reliability and speed up development.

To embarrass people, here is an article by Scott Meyers:
How Non-Member Functions Increase Encapsulation

Remember that in order for a free access function to access data elements of an object, data members must be granted public access, or the function must be a friend of the object. A classic example is overloading stream statements for a class.

0
source share

All Articles