How to get a pointer from a reference?

后端 未结 6 877
野趣味
野趣味 2020-12-07 20:22

There seems to be many relavent questions talking about pointer vs. reference, but I couldn\'t find what I want to know. Basically, an object is passed in by a reference:

6条回答
  •  天命终不由人
    2020-12-07 20:26

    Any operator applied to a reference will actually apply to the object it refers to (§5/5 [expr]); the reference can be thought of as another name for the same object. Taking the address of a reference will therefore give you the address of the object that it refers to.

    It as actually unspecified whether or not a reference requires storage (§8.3.2/4 [dcl.ref]) and so it wouldn't make sense to take the address of the reference itself.

    As an example:

    int x = 5;
    int& y = x;
    int* xp = &x;
    int* yp = &y;
    

    In the above example, xp and yp are equal - that is, the expression xp == yp evaluates to true because they both point to the same object.

提交回复
热议问题