我目前正在处理的一个类有一个定义各种函数的类型的成员。由于各种原因(例如,使其线程安全),我的类应该是这种类型的包装器。无论如何,一些类型的功能可以直接通过,如下所示:
class MyClass {
// ... some functions to work with member_
/* Pass through clear() function of member_ */
void clear() {
member_.clear()
}
private:
WrappedType member_;
};
这还不错,而且我还可以灵活地向 MyClass::clear()
添加更多功能,以备不时之需。然而,如果我有一些这样的传递函数,它会使 MyClass
膨胀,并且对我来说会使它更难阅读。
所以我想知道是否有一种很好的单行方式(除了将上层定义写入一行之外)传递 WrappedType
的成员函数,很像 making base class members available :
/* Pass through clear() in an easier and cleaner way */
using clear = member_.clear; // Unfortunately, this obviously doesn't compile
最佳答案
从您的基类私有(private)继承并使用 using
关键字公开接口(interface)的子集:
class MyClass : private WrappedType
{
public:
using WrappedType::clear;
};
关于c++ - 如何避免传递函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50699150/