[英]How to initialize a member function pointer with member function
我想为我的班级创建一个比较函数(cmp)。 如果构造函数具有一个函数参数,则很好。 如果不是,我想为其设置默认值,有什么想法吗?
下面是我的代码,
template<class T>
class BinaryHeap{
public:
BinaryHeap();
explicit BinaryHeap(bool (*cmp)(T, T));
private:
bool (*cmp)(T ele_a, T ele_b); // function pointer
bool default_cmp(T ele_a, T ele_b);
};
template<class T>
BinaryHeap<T>::BinaryHeap() {
//the code bellow is not work;
this->cmp = default_cmp; // there is problem
}
template<class T>
BinaryHeap<T>::BinaryHeap(bool (*cmp)(T, T)) {
heap_size = 0;
this->cmp = cmp; // this is ok for the compiler
}
只是改变这个
bool default_cmp(T ele_a, T ele_b);
对此
static bool default_cmp(T ele_a, T ele_b);
常规成员函数与函数指针不兼容,但是静态成员函数与之兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.