天天看點

C語言和C++中const的差別

//vs2008下:
#include <stdio.h>
#include <string.h>
int main()
{
	const int n;  //如果字尾改為.cpp的話報錯error C2734: “n”: 如果不是外部的,
				 //則必須初始化常量對象;如果字尾改為.c的話一切正常
	const int i=1;
	int *p=(int *)&i;
	*p=2;
	printf("%d\n",i);  //如果将本源程式的字尾改為.cpp的話,最終輸出的結果為 1,2;
					  //反彙編後這句話為。。。;push 1;。。。 
    				 //如果字尾改為.c的話結果為2,2; 。。。;mov  eax,dword ptr [i] ;
					//push  eax;。。。(注意改字尾後要先儲存,再運作)
	printf("%d\n",*p);

	return 0;
}