【发布时间】:2009-07-13 05:55:17
【问题描述】:
指向 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++ 没有给出警告。这就是我问的原因。
【问题讨论】:
标签: c++ function-pointers