我正在学习 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);
}
最佳答案
这称为混入。参见 CRTP和 this post on mixins .
关于c++关于在自身中嵌入结构名称的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441428/