c++学习----const常量折叠

来源:互联网 发布:无经验美工如何面试 编辑:程序博客网 时间:2024/05/17 08:11
//============================================================================// Name        : Constant_folding.cpp// Author      : gwwu// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================/* 1. i,j 地址相同,指向同一块空间 * 2. i,j指向同一块内存,但是*j = 1修改内存后, *    按理说*j == 1, i 也应该==1,但是实验结果是为0. *    因为i是可折叠常量,在编译阶段对i的引用已经替换为i的值了 *    也就是说 cout << i << endl; 在编译后就被替换为 *    cout << 0 << endl;*/#include <iostream>using namespace std;int main() {    const int i = 0;    int *j = (int*)&i;    *j = 1;    cout << "&i = " << &i <<endl;    cout << "j = " << j << endl;    cout << "i = " << i <<endl;    cout << "*j = " << *j << endl;    return 0;}

编译运行:

&i = 0x28ff28j = 0x28ff28i = 0*j = 1
原创粉丝点击