我现在正在我的大学学习C ++和OOP的基础知识。我不确定100%分配函数给函数指针时如何工作。我遇到了以下代码:
void mystery7(int a, const double b) { cout << "mystery7" << endl; }
const int mystery8(int a, double b) { cout << "mystery8" << endl; }
int main() {
void(*p1)(int, double) = mystery7; /* No error! */
void(*p2)(int, const double) = mystery7;
const int(*p3)(int, double) = mystery8;
const int(*p4)(const int, double) = mystery8; /* No error! */
}
Run Code Online (Sandbox Code Playgroud)
从我的理解来看,p2和p3分配很好,因为函数参数类型匹配并且const-ness是正确的。但是,为什么p1和p4分配失败?将const double / int与非const double / int匹配是否不合法?