简体   繁体   中英

c++ internal helper class

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.

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? ) 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 . 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. 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 . 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;
// ...
}

// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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