我在挣扎于这段代码:
typedef shared_ptr<node <T>> (node<T>::*son_getter)();
son_getter get_son[] = {&node<T>::getLeftSon, &node<T>::getRightSon};
insert = node->*get_son[index]();我得到一个编译错误:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘get_son[index] (...)’, e.g. ‘(... ->* get_son[index]) (...)’
insert = node->*get_son[index]();其中node是shared_ptr<node<T>>,就像insert一样。
我试过所有我能猜到的,但仍然不知道发生了什么。
发布于 2016-05-27 20:11:50
首先,函数调用操作符()比->*具有更高的优先级,因此,您需要添加括号以强制执行所需的计算顺序。此外,node是一个智能指针,而指向成员函数的指针引用存储在该共享指针中的类型。
尽管如此,您还是需要使用以下备选方案之一:
(*node.*get_son[index])();
(&*node->*get_son[index])(); // or std::addressof(*node)->*
(node.get()->*get_son[index])();https://stackoverflow.com/questions/37491224
复制相似问题