Free function versus member function

只谈情不闲聊 提交于 2019-12-18 18:53:42

问题


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? Thanks!

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 fucntion
 }

If you prefer making them members then what if I have a case that 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?


回答1:


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."




回答2:


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.




回答3:


The primary advantage of free functions vs member functions is that it helps decouple the interface from the implementation. For example, std::sort doesn't need to know anything about the underlying container on which it operates, just that it's given access to a container (through iterators) that provide certain characteristics.

In your example the DoSomething2 method doesn't do much to decrease coupling since it still has to access the private member by having it passed by reference. It's almost certainly more obvious to just do the state mutation in the plain DoSomething method instead.

When you can implement a task or algorithm in terms of a class's public interface then that makes it a good candidate to make a free function. Scott Meyers summarizes a reasonable set of rules here: http://cpptips.com/nmemfunc_encap



来源:https://stackoverflow.com/questions/21028773/free-function-versus-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!