他们有什么意义?
我从来没有把它们用于任何东西,我看不出自己需要使用它们.
我错过了关于他们的事情还是他们几乎没用?
编辑:我对它们了解不多,所以可能需要对它们进行描述......
Chr*_*ung 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
[ 下载 ]
它是一个简单的RPN计算器,使用复数而不是实数(主要是因为它std::complex是具有重载运算符的类类型).我用clang测试了这个; 您的里程可能因其他平台而异.
输入应该是形式(0,1).空格是可选的,但可以添加以便于阅读.