这个typedef是必需的吗
Is this typedef required?
有没有一种方法可以在不使用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;
事实上,第二个块不会编译,因为const
使bpr成为对只读指针的引用,而不是对读写指针的常量引用。我有点希望括号能救我:
const (int*)& bpr = bp;
但没有运气。那么,我是否有来typedef指针类型,以便创建对读写指针的常量引用?
使用螺旋规则:
int* const &bpr = bp;
这被读取为bpr是指向int的常量指针的引用。
有关示例,请参见此处。
感谢dasblinkenlight指出原来答案中的括号是不需要的。
相关文章:
- C++:TypeDef使用元组
- 有人能分解一下这个c++模板的语法吗
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 为什么在这个代码结束循环中没有得到结束
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何在BST的这个简单递归实现中消除警告
- 这个typedef和转换运算符语法是什么意思
- 这个Typedef试图做的事情以及如何使用
- 这个 typedef 语句中的语法在做什么
- 这个 typedef 定义是什么意思
- 这个typedef是必需的吗
- 如何简化这个typedef不同的switch语句
- 这个typedef的意思是什么
- 这个typedef行是什么意思
- 这个typedef是什么意思,它有什么好处
- 这个typedef语句做什么?
- typedef Vec<int, 2> Vec2i; 这个定义是什么意思?
- 为什么这个特性类不起作用,来测试一个类是否有某个typedef
- 这个 typedef 语法是如何工作的..