4

他们有什么意义?
我从来没有将它们用于任何事情,而且我根本看不到自己需要使用它们。
我错过了他们的一些东西还是他们几乎没用?

编辑:我对它们了解不多,因此可能需要对它们进行描述...

4

3 回答 3

10

PMF(指向成员函数的指针)类似于普通(静态)函数指针,除了因为非静态成员函数需要this指定对象,PMF 调用语法(.*->*)允许this指定对象(在左侧-手边)。

下面是一个正在使用的 PMF 示例(请注意.*正在使用运算符的“魔术”行:(lhs.*opit->second)(...),以及创建 PMF 的语法,&class::func):

#include <complex>
#include <iostream>
#include <map>
#include <stack>
#include <stdexcept>
#include <string>

namespace {
    using std::cin; using std::complex; using std::cout;
    using std::invalid_argument; using std::map; using std::stack;
    using std::string; using std::underflow_error;

    typedef complex<double> complexd;
    typedef complexd& (complexd::*complexd_pmf)(complexd const&);
    typedef map<char, complexd_pmf> opmap;

    template <typename T>
    typename T::reference top(T& st) {
        if (st.empty())
            throw underflow_error("Empty stack");
        return st.top();
    }
}

int
main()
{
    opmap const ops{{'+', &complexd::operator+=},
                    {'-', &complexd::operator-=},
                    {'*', &complexd::operator*=},
                    {'/', &complexd::operator/=}};

    char op;
    complexd val;
    stack<complexd> st;

    while (cin >> op) {
        opmap::const_iterator opit(ops.find(op));
        if (opit != ops.end()) {
            complexd rhs(top(st));
            st.pop();
                                        // For example of ->* syntax:
            complexd& lhs(top(st));     // complexd* lhs(&top(st));
            (lhs.*opit->second)(rhs);   // (lhs->*opit->second)(rhs);
            cout << lhs << '\n';        // cout << *lhs << '\n';
        } else if (cin.unget() && cin >> val) {
            st.push(val);
        } else {
            throw invalid_argument(string("Unknown operator ") += op);
        }
    }
}

[下载]

它是一个使用复数而不是实数的简单 RPN 计算器(主要是因为std::complex它是具有重载运算符的类类型)。我已经用clang测试过这个;您的里程可能会因其他平台而异。

输入应为(0,1). 空格是可选的,但可以添加以提高可读性。

于 2009-04-14T07:30:30.313 回答
4

将指针绑定到函数在各种情况下都非常有用。基本上,它允许您将函数称为变量,这使您可以在运行时选择要调用的函数。

一种用途是在“回调”中。假设我想让一些后台进程工作一段时间,并让我们知道它什么时候完成(这样我们就可以更新 GUI 或其他东西)。但有时,我们可能希望这个后台进程调用一个方法,有时,我们希望它调用不同的方法。与其编写此后台进程的两个版本,不如编写它,以便后台进程接收指向我们希望它“回调”的函数的指针。然后,当该过程完成时,它首先调用它给出的任何函数。

基本上,它只是让您在决定调用哪个方法时拥有更大的灵活性。这样,它与多态非常相似。事实上,在幕后,我相信 C++ 使用指向函数的指针来促进多态性(通过为每个类存储指向函数的不同指针表)

于 2009-04-14T07:08:35.497 回答
1

如果我正确理解你的问题。为什么不?

struct test
{
    test* operator->() 
    { 
        std::cout << "test::operator->";
        return this; 
    }
};

test tt;
boost::bind( &test::operator ->, _1 )( tt );
于 2009-04-14T07:29:30.597 回答