C++: Difference Between Member and Non Member Functions

C++: Difference Between Non-Member Function and Static Member Function?

static member functions can access private and protected sections of a class. Non-member functions cannot do that as default. They can do that only if a class grants them friendship.

Another point to consider is that name of the static member functions are in the scope of the class. Multiple classes can have static member functions with the same name without worrying about names conflicting.

Member function vs. nonmember function?

When to make it a member function:

  • when the function is logically coupled with the class (like your maze connectedness example)
  • when the function needs to access private or protected members, it's better to make it a member than a friend.

When to make it a standalone function

  • when it's a generic function that can be templatized to naturally work on other classes (look at the <algorithms> header for good example)

How should non-member functions be declared and defined in the .h file and cpp file?

non-member function or "free function" is a function that lives outside of a class.
To create a function like that you can just declare it outside of the class.

.h

Class C
{
int func1(){}; //member function declared and defined in header
int func2(); //member function declared in header
...
}

int func(){}; //free-function declared and defined in header file
int func2(); //free-function declared in header file

.cpp

int C::func2(){} //definition of member function
int func2(){} //definition of free-function
int func3(){} //free-function that is declared and defined in the source file. can only be used within this file

You can see that I used the same function names for both the member function and the free-functions, that is fine because they live in a different namespace.

Is using non-member function a good practice?

Personally, I'd write similarLogic in an anonymous namespace within MyClass.cpp. In that way it's invisible outside that translation unit:

namespace {
int similarLogic(int p_num){
return 5 + p_num;
}
}

Other approaches such as a private member of the class (static or otherwise) pollute the class definition. But that doesn't make the approach a poor choice. If the function requires class member data in the future, then refactoring is simpler than it would be with my favoured way.

Whatever you end up doing, polluting the global namespace is not desirable.

Class of std::vector vs non-member functions (along with a typedef)

Non-member functions are the right thing in this instance. See Scott Meyer's article on the topic How Non-Member Functions Improve Encapsulation.

Also, please do use typedef std::vector<MyClass> someTypeName; You don't want std::vector<MyClass> littered throughout your code. You want what the type is not how it's implemented. If you ever have to change the implementation to use a different container, you will be quite glad you used a typedef.

ETA: in comments I am reminded of using and its superiority to typedef.

using someTypeName = std::vector<MyClass>; 


Related Topics



Leave a reply



Submit