简体   繁体   English

C++ 类“辅助函数”应该是成员、自由的还是非命名空间自由的?

[英]should C++ class "helper functions" be members, free, or anon-namespace free?

So, I have a class.所以,我有一个类。 It's a useful class.这是一个有用的类。 I like a lot.我非常喜欢。 Let's call it MyUsefulClass .我们称它为MyUsefulClass MyUsefulClass has a public method. MyUsefulClass有一个公共方法。 Let's call it processUsefulData(std::vector<int>&) .我们称它为processUsefulData(std::vector<int>&)

Now suppose processUsefulData really does two things and I want to refactor it from this:现在假设processUsefulData确实做了两件事,我想从中重构它:

std::vector<int> MyUsefulClass::processUsefulData(std::vector<int>& data)
{
    for (/*...*/)
    {
        for (/*...*/)
        {
            // a bunch of statements...
        }
    }

    for (/*...*/)
    {
        for (/*...*/)
        {
            // a bunch of other statements...
        }
    }
    return data;
}

Now, I want to split these responsibilities and rewrite the code as现在,我想拆分这些职责并将代码重写为

std::vector<int> MyUsefulClass::processUsefulData(std::vector<int>& data)
{
    doProcessA(data, dataMember_);
    doProcessB(data, otherDataMember_);
    return data;
}

So, I don't know if I should make the two helper functions free functions or member functions, and when each would be appropriate.所以,我不知道我是否应该将这两个辅助函数设为自由函数或成员函数,以及何时合适。 I also don't know if it's better to make them in an anonymous namespace or not.我也不知道将它们放在匿名名称空间中是否更好。 Does anyone know good times to do this?有谁知道这样做的好时机吗?

I generally make helper routines "free" routines in an anonomous namespace if possible.如果可能的话,我通常在匿名命名空间中使辅助例程成为“免费”例程。 That way I don't complicate the interface (off in the *.h file) with stuff clients don't need to worry about.这样我就不会因为客户不需要担心的事情而使接口(在 *.h 文件中关闭)复杂化。

However, you have to be careful that you don't introduce non-reentrancy by doing that.但是,您必须小心不要通过这样做引入不可重入性。 For instance, by modifying global data objects or static locals rather than class members.例如,通过修改全局数据对象或静态局部变量而不是类成员。 If you need to do that, you are better off making it a proper class member.如果你需要这样做,你最好让它成为一个合适的班级成员。

Free function / member function自由函数/成员函数

I would make them free functions is possible (they do not need access to the internals of the class).我会让它们成为可能的自由函数(它们不需要访问类的内部结构)。 If they work on a set of attributes or need access to other members then make it a member function.如果他们处理一组属性或需要访问其他成员,则将其设为成员函数。

Access使用权

If the code only has sense in this scope, and will not be used from other code then make them private : private if it is a member, or implemented in an unnamed namespace if it is a free function.如果代码仅在此范围内有意义,并且不会被其他代码使用,则将它们设为私有:如果它是成员,则为私有;如果它是自由函数,则在未命名的命名空间中实现。

If other code will benefit from using the code then publish it in the interface.如果其他代码将从使用该代码中受益,则将其发布在界面中。 That means making it protected if it is a member or having the free function accessible through a header in a named namespace (or global namespace).这意味着如果它是成员,则使其受到保护,或者可以通过命名空间(或全局空间)中的标头访问自由函数。

I usually make them protected or private member functions.我通常将它们protectedprivate的成员函数。 It would depend on whether you plan on deriving the class and overriding the functions.这将取决于您是否计划派生类并覆盖函数。

If they are common enough functions that they are used in other classes, move them to static functions contained in a common class or a separate object that your class uses.如果它们是在其他类中使用的足够常见的函数,请将它们移动到包含在公共类或您的类使用的单独对象中的静态函数。

Always prefer free functions over member ones.总是喜欢免费功能而不是会员功能。 See my answer here to know why. 在这里查看我的答案以了解原因。

The fact that you mention free functions leads me to believe that the 'bunch of other statements' do not require access to class data.您提到自由函数这一事实使我相信“其他语句”不需要访问类数据。 If so, make them free.如果是这样,让他们自由。 This reduces complexity of your class header, plus free functions are easier to use in the standard algorithms (maybe std::for_each since you're working with vectors anyway?).这降低了类头的复杂性,而且自由函数在标准算法中更容易使用(也许是 std::for_each ,因为你无论如何都在使用向量?)。

Think about the scope.考虑范围。 Are those functions going to be used in another class, or elsewhere?这些功能是否将在另一个类或其他地方使用? Should they be publically call-able?他们应该可以公开调用吗?

It seems like they should be private member functions to me, but it all depends on your overall scoping structure.对我来说,它们似乎应该是私有成员函数,但这完全取决于您的整体范围结构。

Member functions certainly if the original function made sense as a member function.如果原始函数作为成员函数有意义,那么当然是成员函数。

Private/protected IMHO depends on how their functionality is used: if the original function's operation is still required and the refactor is solely to make the code cleaner then make them protected or private and call them from the regular function.私有/受保护 恕我直言,取决于它们的功能是如何使用的:如果仍然需要原始函数的操作,并且重构只是为了使代码更清晰,那么将它们设置为受保护或私有,并从常规函数中调用它们。 You get the refactor but keep the class's public interface intact that way.你得到了重构,但保持类的公共接口不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM