How to get a pointer from a reference?

后端 未结 6 873
野趣味
野趣味 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:23

    The general solution is to use std::addressof, as in:

    #include 
    
    void foo(T & x)
    {
        T * p = std::addressof(x);
    }
    

    This works no matter whether T overloads operator& or not.

提交回复
热议问题