简体   繁体   English

解引用指针并存储对指向数据的引用,然后获取引用地址并对其执行指针运算。 c++

[英]Dereferencing pointer and storing reference to the pointed data, then getting address of reference and performing pointer arithmetic on it. c++

In order to make a diagram(of my own, to understand pointers/pointers-to-pointers/arrays) that is out of the scope this question, I want to know if it is adequate to assume the following:为了制作一个不属于 scope 这个问题的图表(我自己的,以理解指针/指向指针/数组的指针),我想知道假设以下内容是否足够:

int *intptr = new int[10]{0};
int &intref = *intptr;
int thirdInt = *((&intref)+2);//is this defined behavior and always equivalent to intptr[2]?????
delete [] intptr;

as the only comment in the code asks: does this code invoke undefined behavior or not work as expected by the comment?正如代码中唯一的注释所问的那样:此代码是调用未定义的行为还是没有按照注释的预期工作?

then getting address of reference然后获取参考地址

To clarify this bit: It is not possible to "get address of a reference".为了澄清这一点:不可能“获取引用地址”。 When you apply addressof operator on a reference, you are getting the address of the referred object.当您在引用上应用 addressof 运算符时,您将获得引用的 object 的地址。

this code invoke undefined behavior此代码调用未定义的行为

No.不。

always equivalent to intptr[2]?????总是等价于 intptr[2]?????

Yes (except in case of a class type with strange overload for operator &, but those are and should be hardly ever used).是的(除了 class 类型对运算符 & 有奇怪的重载的情况,但这些都是并且应该几乎不会使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM