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: 
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.
You can read these following sites for this purpose [PIMPL](http://c2.com/cgi/wiki?PimplIdiom), [Opaque pointer](https://en.wikipedia.org/wiki/Opaque_pointer). 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;
// ...
}

// ...

```