小编Ala*_*ian的帖子

为什么不能为常量分配一个字符串字面量引用指针

我可以使指针指向字符串文字,它将字符串文字转换为常量指针,但对指针的引用不能分配字符串文字,例如:

 const char *&text = "Hello, World\n"; 
Run Code Online (Sandbox Code Playgroud)

有一个错误,编译器说我不能将数组转换为指针引用,我很好奇,为什么它不正确?

c++ pointers reference constants const-pointer

5
推荐指数
2
解决办法
379
查看次数

为什么在通过指针编译时不能分配 const 初始化

我很好奇为什么我的研究结果很奇怪

#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++

5
推荐指数
1
解决办法
123
查看次数

如果 const 不分配内存,为什么我可以获得 const 的地址?

C++ Primer说如果const可以在编译时初始化,它会将标识符交换为初始化时使用的值,那么为什么我可以在编译时获得const initialize的地址?

c++

3
推荐指数
1
解决办法
77
查看次数

标签 统计

c++ ×3

const-pointer ×1

constants ×1

pointers ×1

reference ×1