Question: I would like to ask question regarding internal helper class in 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.

Answer: 
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.

It has the downside of basically showing your underwear to everyone who needs to #include your header.

* Using the Pimpl idiom.

You declare a second class, the "Private Implementation" (<https://en.wikipedia.org/wiki/Opaque_pointer>, [Is the pImpl idiom really used in practice?](https://stackoverflow.com/questions/8972588/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.

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. I would recommend this method ahead of the next method.

In the .cpp file:

```
// 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](https://en.wikipedia.org/wiki/Opaque_pointer). 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.