[英]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.