Is there a name for the idiom of using a friend class to hide what would have been private functions?

  c++

If a class has lots of private functions it seems undesirable to have them declared in the header file. It makes it harder for clients to parse the interface of the class and it increases compilation time.

One option is, of course, the pimpl idiom, but that adds a layer of indirection and also results in a memory allocation.

What follows is a example of hiding helper functions in a friend class that is defined in the implementation file.

Is there a name for this idiom?

Is there any reason not to do it?

//Foo.h
class Foo
{
public:
    void FuncA();
    void FuncB();
    
private:
    int x;
    // more data...

    friend struct FooHelpers;
};
//Foo.cpp
struct FooHelpers
{
    static void Helper1(Foo& f)
    {
        // manipulate private data..
        f.x++; //etc.
    }
    // Possibly more funcs...
};

void Foo::FuncA()
{
    //....
    FooHelpers::Helper1(*this);
    //....
}
//....

Source: Windows Questions C++

LEAVE A COMMENT