c++ - 如何避免传递函数

标签 c++ wrapper

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

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/

相关文章:

包含 return 的 C++ 宏表达式(就像 Rust 的 try!)

c++ - 如何解析cpprestsdk生成的多个Set-Cookie?

c++ - g++对齐问题

java - 从几乎与接口(interface)匹配的类动态创建实现?

c# - 在 C# 中使用运行时确定的 C++ DLL

c++ - std::vector 函数 push_back 中的奇怪段错误

c++ - Swift 中是否有等效的 decltype?

c++ - 自定义引用类型

c++ - C++ 中带有 Wrapper 函数的 GSL 错误处理程序状态

java - java中的异常或null