Helper function: static in cpp or define as static helper class method [closed]

给你一囗甜甜゛ 提交于 2020-01-15 16:41:08

问题


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:

  1. define a helper class with static method(s) and use it
  2. 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

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