问题
I need to write a helper function(s) that will be used in a class method. Target is a static library. I know the following about the usage:
- it will not use this class members
- it will not be used in other classes
I see two general ways to resolve it:
- define a helper class with static method(s) and use it
- define it as a
static
function in cpp file
In the first case (as far as I understand) these functions will be visible in library symbols list. In the second case they will not be visible but it doesn't seem to be a good practice. Which way should I choose?
回答1:
"helper class with static method(s)" is a Java-ism that has no place in C++.
A static function at file scope is a C-ism.
The modern C++ solution (only a couple of decades old) that you should choose is a free function in an anonymous namespace:
namespace
{
void helperfunction() {}
}
void Class::function()
{
helperfunction();
}
来源:https://stackoverflow.com/questions/34136057/helper-function-static-in-cpp-or-define-as-static-helper-class-method