我可以使指针指向字符串文字,它将字符串文字转换为常量指针,但对指针的引用不能分配字符串文字,例如:
const char *&text = "Hello, World\n";
Run Code Online (Sandbox Code Playgroud)
有一个错误,编译器说我不能将数组转换为指针引用,我很好奇,为什么它不正确?
我很好奇为什么我的研究结果很奇怪
#include <iostream>
int test()
{
return 0;
}
int main()
{
/*include either the next line or the one after*/
const int a = test(); //the result is 1:1
const int a = 0; //the result is 0:1
int* ra = (int*)((void*)&a);
*ra = 1;
std::cout << a << ":" << *ra << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么常量 var initialize while runtime 可以完全改变,而 initialize while compile 只会改变指针的 var?
C++ Primer说如果const可以在编译时初始化,它会将标识符交换为初始化时使用的值,那么为什么我可以在编译时获得const initialize的地址?