有没有一种方法可以在不使用 typedef 的情况下完成此代码块在 C++ 中的作用?
typedef int* pointer;
int a = 3;
int* ap = &a;
const pointer& apr = ap;
*apr = 4;
这不行:
int b = 3;
int* bp = &b;
const int*& bpr = bp;
*bpr = 4;
事实上,第二个 block 不会编译,因为 const
使 bpr 成为对只读指针的引用,而不是对读写指针的 const 引用。我有点希望括号能救我:
const (int*)& bpr = bp;
...但运气不好。那么我必须对指针类型进行类型定义,以便创建对读写指针的常量引用吗?
最佳答案
使用 spiral rule :
int* const &bpr = bp;
这被读作 bpr 是对指向 int 的常量指针的引用。
有关示例,请参阅 here .
感谢 dasblinkenlight 指出原始答案中的括号不是必需的。
关于c++ - 这个typedef是必需的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308925/