c++关于在自身中嵌入结构名称的困惑

标签 c++ inheritance boost struct

我正在学习 boost,这里有一段代码来自:http://www.boost.org/doc/libs/1_55_0/libs/graph/example/visitor.cpp struct edge_printer的定义让我很困惑,它继承了base_visitor,但是base_visitor模板的类型名被指定为edge_printer 本身。请问这个在C++里叫什么?

template <class Tag>
struct edge_printer : public base_visitor<edge_printer<Tag> > {
  typedef Tag event_filter;
  edge_printer(std::string edge_t) : m_edge_type(edge_t) { }
  template <class Edge, class Graph>
  void operator()(Edge e, Graph& G) {
    std::cout << m_edge_type << ": " << source(e, G) 
              << " --> " <<  target(e, G) << std::endl;
  }
  std::string m_edge_type;
};
template <class Tag>
edge_printer<Tag> print_edge(std::string type, Tag) { 
  return edge_printer<Tag>(type);
}

最佳答案

这称为混入。参见 CRTPthis post on mixins .

关于c++关于在自身中嵌入结构名称的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441428/

相关文章:

c++ - 在不使用变量的情况下实例化 ifstream 对象

c# - 如何使用 GetType 访问 C# 中派生类的方法/属性?

C# 包含继承类的基类数组,访问非继承字段

c++ - 使用 boost::iterator_facade 的优点和缺点是什么?

c++ - 创建没有可调用对象的 boost::thread

c++ - 如何启动一个运行成员函数的 boost::thread?

c++ - C++中根据字符串创建对象

c++ - 从 Qt 字符串打开文件

css - 覆盖没有默认值的 CSS 属性

c++ - memmem() STL 方式?