const关键字是c++中一个很重要又很有迷惑性的知识点,这里对其进行一次总结。
const修饰非成员变量
const全局/局部变量
const全局变量
在文件a.cpp中定义了一个全局变量a
int a = 1;
在文件test.cpp中使用全局变量a
#include <iostream>
using namespace std;
extern int a;
int main()
{
//const volatile int a = 7;
int *p = (int *)(&a);
*p = 8;
cout << "a=" << a << endl;
cout << "*p=" << *p;
system("pause");
return 0;
}
结果为:
如果将全局变量a定义为const
const int a = 1;
#include <iostream>
using namespace std;
extern const int a