code>要指定的对象,PMF调用语法(。* 或 - > * )allow 这是一个使用中的PMF示例(注意使用。* 运算符的魔术行:(lhs。* opit-> second)(...) / code>,以及创建PMF & class :: func 的语法: #include< complex> #include< iostream> #include< map> #include< stack> #include< stdexcept> #include< string> 命名空间{ using std :: cin;使用std :: complex;使用std :: cout; using std :: invalid_argument;使用std :: map;使用std :: stack; using std :: string;使用std :: underflow_error; typedef complex< double>复杂 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(); //例如 - > *语法: complexd& lhs(top(st)); // complexd * lhs(& top(st)); (lhs。* opit-> second)(rhs); //(lhs-> * opit-> second)(rhs); cout<< lhs '; // cout<< * lhs<< '\\\'; } else if(cin.unget()&& cin>> val){ st.push(val); } else { throw invalid_argument(string(Unknown operator)+ = op); } } } [下载] 这是一个使用复数而不是实数的简单RPN计算器(主要是因为 std :: complex 是具有重载运算符的类类型)。我已经用 clang 测试过了; 输入应该是(0,1)的形式。空格是可选的,但可以添加为可读性。 What is the point of them?I've never used them for anything, and I can't see myself needing to use them at all.Am I missing something about them or are they pretty much useless?EDIT: I don't know much about them, so a description about them might be necessary... 解决方案 A PMF (pointer to member function) is like a normal (static) function pointer, except, because non-static member functions require the this object to be specified, the PMF invocation syntax (.* or ->*) allow the this object to be specified (on the left-hand side).Here's an example of PMFs in use (note the "magic" line with the .* operator being used: (lhs.*opit->second)(...), and the syntax for creating a 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(); }}intmain(){ 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); } }}[Download]It's a simple RPN calculator using complex numbers instead of real numbers (mostly because std::complex is a class type with overloaded operators). I've tested this with clang; your mileage may vary with other platforms.Input should be of the form (0,1). Spaces are optional, but can be added for readability. 这篇关于在C ++中绑定成员操作符的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 14:59