簡體   English   中英

傳遞給 C++ STL mem_fun_t 的對象如何調用其名稱在 mem_fun_t 中定義的函數

[英]how could the object passed to C++ STL mem_fun_t call the function whose name is defined inside mem_fun_t

這是來自 GNU STL 的代碼:

template<typename _Ret, typename _Tp>
    class mem_fun_t : public unary_function<_Tp*, _Ret>
    {
    public:
      explicit
      mem_fun_t(_Ret (_Tp::*__pf)())
      : _M_f(__pf) { }

      _Ret
      operator()(_Tp* __p) const
      { return (__p->*_M_f)(); }

    private:
      _Ret (_Tp::*_M_f)();
    };

雖然__p有有同類型的成員函數_M_f ,怎么可能__p調用_M_f__p沒有命名的成員_M_f

_M_f是指向成員函數的指針,類型為_Ret(_Tp::*)()

它通過指針調用: __p->*_M_f)() 取消引用運算符*在那里非常重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM