如何避免传递函数

Kor*_*ose 2 c++ wrapper

我目前正在处理的一个类有一个定义各种函数的类型的成员.由于各种原因,我的类应该是这种类型的包装器(例如,使其成为线程安全的).无论如何,一些类型的函数可以通过,如下所示:

class MyClass {
  // ... some functions to work with member_

  /* Pass through clear() function of member_ */
  void clear() {
    member_.clear()
  }

private:
  WrappedType member_;
};
Run Code Online (Sandbox Code Playgroud)

这并不是那么糟糕,而且我还可以灵活地添加更多功能,MyClass::clear()以备不时之需.然而,如果我有一些这些传递函数,它会膨胀MyClass,对我来说会让它更难阅读.

所以我想知道是否有一个很好的单行方式(除了将上层定义写成一行)传递WrappedType的成员函数,就像使基类成员可用:

/* Pass through clear() in an easier and cleaner way */
using clear = member_.clear; // Unfortunately, this obviously doesn't compile
Run Code Online (Sandbox Code Playgroud)

Vit*_*meo 6

私有地从您的基类继承并使用using关键字公开接口的子集:

class MyClass : private WrappedType 
{
public:
    using WrappedType::clear; 
};
Run Code Online (Sandbox Code Playgroud)