代码之家  ›  专栏  ›  技术社区  ›  sharptooth

如果运算符重载C++类,我怎么可以使用默认操作符呢?

  •  5
  • sharptooth  · 技术社区  · 15 年前

    _com_ptr_ 具有带有副作用的重载运算符&()。如果我有一个变量:

    _com_ptr_t<Interface> variable;
    

    如何在不调用重载运算符并触发副作用的情况下检索其地址(_com_ptr_t<interface>*指针)?

    3 回复  |  直到 10 年前
        1
  •  7
  •   MSalters    15 年前

    我在一次ISO会议上看到了这个案例,因为它破坏了一些offsetof()宏实现(lwg 273)。解决方案: &reinterpret_cast<unsigned char&>(variable)

        2
  •  3
  •   Daniel Earwicker    15 年前

    我定义这个实用程序函数:

    template<typename T>
    T *GetRealAddr(T &t)
        { return reinterpret_cast<T*>(&reinterpret_cast<unsigned char &>(t)); }
    
        3
  •  0
  •   Goz    15 年前

    &variable.getInterfacePtr();