简体   繁体   English

C++ 内部助手类

[英]c++ internal helper class

I would like to ask question regarding internal helper class in C++.我想问一下关于 C++ 中的内部助手类的问题。 What is the best way to structure this?构建此结构的最佳方法是什么?

Let me clarify what do I mean by internal helper class by example.让我通过示例阐明内部帮助程序类的含义。

// MyClass.h
class MyClass
{
    int myData;
    bool isSomething;
    ...
public:
    void DoSomething();
};

// MyClass.cpp
// This is what I mean by internal helper function. Helper function that's only visible int the implementation file (.cpp) but requires access to the private members of the class.
static void DoSomethingInternal( MyClass *myClass )
{
   // Access myClass private members
}

void MyClass::DoSomething()
{
    ...
    DoSomethingInternal(this);
    ...
}

I know that declaring friend function can be a solution.我知道声明朋友函数可以是一个解决方案。 However, it makes the class declaration ugly.然而,它使类声明变得丑陋。 In addition, for every new helper function, I have to add a friend function.另外,对于每一个新的辅助函数,我都必须添加一个友元函数。

Is there an idiom/design pattern for this?有这方面的习惯用法/设计模式吗? I have been searching in the Internet, but didn't find any.我一直在互联网上搜索,但没有找到任何。

Thank you in advance.先感谢您。 Your answers are greatly appreciated.非常感谢您的回答。

In my experience, a lot of dev teams have no problem with static local helper functions, it helps reduce header bloat, helps keep the formally exposed interface smaller, and so forth.根据我的经验,许多开发团队对静态本地辅助函数没有任何问题,它有助于减少标题膨胀,有助于保持正式公开的界面更小,等等。 It has the advantage of being lightweight, it has the disadvantage that it can lead to friend bloat/pollution if you are using lots of private members and no accessors.它的优点是轻量级,缺点是如果您使用大量私有成员且没有访问器,则可能导致朋友膨胀/污染。

But within the discussion community it is generally frowned upon in favor of the following.但是在讨论社区中,它通常不赞成以下内容。

  • Declaring helpers as private member functions.将助手声明为私有成员函数。

This has the advantage of clearly associating fn _doThingsForFoo(Foo*) with Foo , and saving you from a lot of headaches exposing private members.这具有将 fn _doThingsForFoo(Foo*)Foo明确关联的优点,并使您免于暴露私有成员的许多麻烦。

It has the downside of basically showing your underwear to everyone who needs to #include your header.它的缺点是基本上向需要#include您的标题的每个人展示您的内衣。

  • Using the Pimpl idiom.使用 Pimpl 成语。

You declare a second class, the "Private Implementation" ( https://en.wikipedia.org/wiki/Opaque_pointer , Is the pImpl idiom really used in practice? ) and you put all of the private stuff you don't want in the main header into that.您声明了第二个类,即“私有实现”( https://en.wikipedia.org/wiki/Opaque_pointer,pImpl 习语真的在实践中使用了吗? ),然后将所有您不想要的私有内容放入主标题到那个。

It has the advantage of allowing you to hide your stuff, it has the disadvantage of adding an extra pointer to feed, store and traverse (oh and free).它的优点是允许你隐藏你的东西,它的缺点是添加一个额外的指针来馈送、存储和遍历(哦,免费)。

There are couple of ways to accomplish that.有几种方法可以实现这一点。

  1. Use a helper class/function in the .cpp file if the helper functions don't need access to the data directly.如果辅助函数不需要直接访问数据,请在 .cpp 文件中使用辅助类/函数。 I would recommend this method ahead of the next method.我会在下一个方法之前推荐这种方法。

    In the .cpp file:在 .cpp 文件中:

     // Create a namespace that is unique to the file namespace MyClassNS { namespace HelperAPI { void DoSomethingInternal(MyClass* obj) { ... } } } using namespace MyClassNS; void MyClass::DoSomething() { ... // HelperAPI::DoSomethingInternal(this); ... }
  2. Use the pimple idiom .使用疙瘩成语 When using this idiom, you can add any number of helper functions in the private data class without touching the public interface of the class.使用这个习惯用法时,您可以在私有数据类中添加任意数量的辅助函数,而无需触及该类的公共接口。

The design pattern is simple: don't use helper classes .设计模式很简单: 不要使用辅助类 If a class should do something, let it do it itself.如果一个类应该做某事,让它自己做。

As per the upvoted answer given by StenSoft, you should implement the methods inside the class.根据 StenSoft 给出的赞成答案,您应该在类中实现这些方法。 However, if that is not an option for some reason, then use helpers.但是,如果由于某种原因这不是一个选项,那么请使用助手。 If even that is not an option, then use reflection.如果这不是一个选项,那么使用反射。 If even that is not an option, then use a command listener inside your class.如果这不是一个选项,那么在您的类中使用命令侦听器。 If even that is not an option, then watch a tutorial.如果这不是一个选项,那么观看教程。

You can read these following sites for this purpose PIMPL , Opaque pointer .为此,您可以阅读以下网站PIMPL不透明指针 With this you only need to have one member variable and you can put all private things into the private class.有了这个,你只需要一个成员变量,你就可以把所有私有的东西放到私有类中。

your header:你的标题:

class PrivateClass;

class Class
{
public:
    // ...

private:
    PrivateClass* m_Private;
};

your source:你的消息来源:

class PrivateClass
{
// ...
};

Class::Class
: m_Private( new PrivateClass )
{
// ...
}

UPDATE: I forgot to tell mention to delete the private member in the desctructor.更新:我忘了告诉提及删除析构函数中的私有成员。

Class::~Class
{
    delete m_Private;
// ...
}

// ...

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

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