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
Converting Narrow String to Wide String
How to Get Hwnd of Window Opened by Shellexecuteex.. Hprocess
C++ If Else If Not Working Properly
Undefined Reference to Template Members
How to Print the Value of Nullptr on Screen
Std::Filesystem' Has Not Been Declared After Including <Experimental/Filesystem>
Is Using an Union in Place of a Cast Well Defined
Is There a Case Where Including the Same Header Twice Is Actually Helpful
Std::Stod Throws Out_Of_Range Error for a String That Should Be Valid
Will Memcpy or Memmove Cause Problems Copying Classes
Print Out All Combinations of Index
How to Compare Two Standard Conversion Sequences Use the Rank of Contained Conversions
What Are These Seemingly-Useless Callq Instructions in My X86 Object Files For
How to Invoke a User-Defined Conversion Function via List-Initialization
How to #Include When There Is a Circular Dependency