辅助函数:cpp中为静态或定义为静态辅助类方法 [英] Helper function: static in cpp or define as static helper class method

查看:191
本文介绍了辅助函数:cpp中为静态或定义为静态辅助类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个将在类方法中使用的辅助函数.目标是静态库.我知道以下用法:

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:

  • 它不会使用此类成员
  • 它将不会在其他课程中使用

我看到两种一般的解决方法:

I see two general ways to resolve it:

  1. 使用静态方法定义一个帮助器类并使用它
  2. 在cpp文件中将其定义为static函数

在第一种情况下(据我了解),这些函数将在库符号列表中可见.在第二种情况下,它们将不可见,但这似乎不是一个好习惯.我应该选择哪种方式?

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?

推荐答案

带有静态方法的帮助器类"是一种Java主义,在C ++中没有地位.
文件范围内的静态功能是C-ism.

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

您应该选择的现代C ++解决方案(只有几十年的历史)是匿名名称空间中的自由函数:

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();
}

这篇关于辅助函数:cpp中为静态或定义为静态辅助类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆