指向 const 的指针和函数的常用指针之间有什么区别吗?什么时候适合将 const 限定符用于独立函数?
我写了简短的示例来说明我的问题:
#include <iostream>
using namespace std;
int sum( int x, int y ) { return x + y; }
typedef int sum_func( int, int );
int main()
{
const sum_func* sum_func_cptr = ∑ // const function
sum_func* sum_func_ptr = ∑ // non-const function ?
// What is the difference between sum_func_cptr and sum_func_ptr
int x = sum_func_cptr( 2, 2 );
cout << x << endl;
int y = sum_func_ptr( 2, 2 );
cout << y << endl;
sum_func_cptr = 0;
sum_func_ptr = 0;
return 0;
}
g++ 没有给出警告。这就是我问的原因。