Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free function versus member function

What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class member function free of any parameters and accessing member variables directly?

header:

 Class A {     int myVariable;     void DoSomething() {        myVariable = 1;     }  }; 

source:

 namespace {     void DoSomething2(int &a) {         a = 1;     }  }   int A::SomeFunction() {     DoSomething2(myVariable); // calling free function     DoSomething(); // calling member function  } 

If you prefer making them members, then what if I have a case where I first call a function that is not accessing any member variables, but that function calls another function which is accessing a member. Should they both be member functions or free?

like image 742
Jana Avatar asked Jan 09 '14 18:01

Jana


People also ask

What is the difference between function and member function?

A function declared/defined within a class is a member function and a function declared/defined outside the scope of a class is a function.

What are free functions?

The term free function in C++ simply refers to non-member functions. Every function that is not a member function is a free function.

What is the difference between member function and non-member function?

A member function is declared in the class but defined outside the class and is called using the object of the class. A non-member function that is declared outside the class but called a normal function inside the main function.

When should a function be a member function?

One way to look at this is this: If a function manipulates an object's inner state, then that's a good indication that this function should probably be a member function. If a function uses an object without changing its inner state, then that's a good indication that this function probably should be a free function.


2 Answers

see this question: Effective C++ Item 23 Prefer non-member non-friend functions to member functions and also C++ Member Functions vs Free Functions

You should prefer free functions, in the extent that it promotes loose coupling.

Consider making it a member function only if it works on the guts of your class, and that you consider it really really tied to your class.

It is a point of the book 101 C++ coding standards, which states to prefer free function and static function over member functions.

Altough this may be considered opinion based, it allows to keep class little, and to seperate concerns.

This answer states: "the reason for this rule is that by using member functions you may rely too much on the internals of a class by accident."

like image 186
Stephane Rolland Avatar answered Sep 24 '22 02:09

Stephane Rolland


One advantage of a non-member function in a source file is similar to the benefits of the Pimpl idiom: clients using your headers do not have to recompile if you change your implementation.

// widget.h class Widget  { public:     void meh(); private:     int bla_; };  // widget.cpp namespace {     void helper(Widget* w) // clients will never know about this     { /* yadayada */ } }  void widget::meh()  { helper(this); } 

Of course, when written like this, helper() can only use the public interface of Widget, so you gain little. You can put a friend declaration for helper() inside Widget but at some point you better switch to a full-blown Pimpl solution.

like image 39
TemplateRex Avatar answered Sep 25 '22 02:09

TemplateRex