如何通过函数指针递归调用 class 成员函数?
How can I recursively call a class member function via a function pointer?
我正在为 运行 类树对象的某些算法编写一个库。我有一个 edge_t
class 有 const unsigned int
数据成员 edge_id
和 weight
分别作为 edge_t
的唯一标识符和边的权重。
我用 C++ 编写了 tree_t
和 subtree_t
classes,它们都包含指向 edge_t
s 的指针的映射。 tree_t
和 subtree_t
都是从一个抽象 basic_tree_t
class 派生的,它包含树状对象应该具有的所有功能,包括以下方法:
// returns the sum of the weights of the edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::weight(const edge_ptr) const
// returns the number of edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::num_descendents(const edge_ptr) const
我正在编写一些其他代码,其中用户输入一个 tree_t
对象,代码必须从中迭代地采样一个 subtree_t
,进行一些计算,再采样另一个 subtree_t
,做更多的计算,等等。为了进行计算,代码需要知道每个子树中每条边的 weight
和 num_descendents
的值。
为了避免重复计算相同的值,每次我构建一个新的子树时,我都会创建 std::map<unsigned int, unsigned int> weight_map
和 std::map<unsigned int, unsigned int> num_descendents_map
,它们将子树的边的每个 edge_id
映射到basic_tree_t
中各个成员函数输出的值,然后使用这些值。我编写了以下函数来填充这些地图:
void populate_weight_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & weight_map)
{
weight_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.weight(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_weight_map(S, c, weight_map);
}
void populate_num_descendents_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & num_descendents_map)
{
num_descendents_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.num_descendents(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_weight_map(S, c, num_descendents_map);
}
它们在很大程度上是相同的函数,所以我认为编写一个将指向相关 basic_tree_t
成员函数的指针作为第四个参数的函数更有意义,如下所示:
void populate_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & m, unsigned int (basic_tree_t::*f)(const edge_ptr) const)
{
m.insert(std::pair<unsigned int, unsigned int>(e->edge_id, (S.*f)(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_map(S, c, m, &basic_tree_t::*f); // ERROR ON THIS LINE!
}
但是,编译器 returns 最后一行出现了一个不透明的错误:
error: expected unqualified-id
populate_map(S, c, m, &basic_tree_t::*f);
^
populate map
的第四个参数应该是什么?
f
已经是指向所需成员的指针,因此只需传递:
populate_map(S, c, m, f);
&basic_tree_t::*f
在这种情况下没有意义。它看起来像是试图 声明 一个指向 data 成员的指针,无论如何这不是你想要的。
我正在为 运行 类树对象的某些算法编写一个库。我有一个 edge_t
class 有 const unsigned int
数据成员 edge_id
和 weight
分别作为 edge_t
的唯一标识符和边的权重。
我用 C++ 编写了 tree_t
和 subtree_t
classes,它们都包含指向 edge_t
s 的指针的映射。 tree_t
和 subtree_t
都是从一个抽象 basic_tree_t
class 派生的,它包含树状对象应该具有的所有功能,包括以下方法:
// returns the sum of the weights of the edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::weight(const edge_ptr) const
// returns the number of edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::num_descendents(const edge_ptr) const
我正在编写一些其他代码,其中用户输入一个 tree_t
对象,代码必须从中迭代地采样一个 subtree_t
,进行一些计算,再采样另一个 subtree_t
,做更多的计算,等等。为了进行计算,代码需要知道每个子树中每条边的 weight
和 num_descendents
的值。
为了避免重复计算相同的值,每次我构建一个新的子树时,我都会创建 std::map<unsigned int, unsigned int> weight_map
和 std::map<unsigned int, unsigned int> num_descendents_map
,它们将子树的边的每个 edge_id
映射到basic_tree_t
中各个成员函数输出的值,然后使用这些值。我编写了以下函数来填充这些地图:
void populate_weight_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & weight_map)
{
weight_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.weight(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_weight_map(S, c, weight_map);
}
void populate_num_descendents_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & num_descendents_map)
{
num_descendents_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.num_descendents(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_weight_map(S, c, num_descendents_map);
}
它们在很大程度上是相同的函数,所以我认为编写一个将指向相关 basic_tree_t
成员函数的指针作为第四个参数的函数更有意义,如下所示:
void populate_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & m, unsigned int (basic_tree_t::*f)(const edge_ptr) const)
{
m.insert(std::pair<unsigned int, unsigned int>(e->edge_id, (S.*f)(e)));
for (auto & c : *(e->children))
if (S.contains(c))
populate_map(S, c, m, &basic_tree_t::*f); // ERROR ON THIS LINE!
}
但是,编译器 returns 最后一行出现了一个不透明的错误:
error: expected unqualified-id
populate_map(S, c, m, &basic_tree_t::*f);
^
populate map
的第四个参数应该是什么?
f
已经是指向所需成员的指针,因此只需传递:
populate_map(S, c, m, f);
&basic_tree_t::*f
在这种情况下没有意义。它看起来像是试图 声明 一个指向 data 成员的指针,无论如何这不是你想要的。