2013-02-23 400 views
2

在·的std ::的unique_ptr·文件「記憶」,我看到的運算符重載函數爲&**是什麼意思?

typename tr1::add_reference<_Ty>::type operator*() const 
{ 
    // return reference to object 
    return (*this->_Myptr); 
} 

pointer operator->() const 
{ 
    // return pointer to class object 
    return (&**this); 
} 

代碼什麼是&**在第二個功能是什麼意思?謝謝。

回答

6

this是指向unique_ptr對象的指針。

*this是對unique_ptr對象的引用。

**this使用operator*(即*this->_Myptr)取消參考unique_ptr

因此,&**this是指向由unique_ptr(即&(*this->_Myptr))指向的對象的指針。

5

根據發佈的代碼,**this正在調用operator*重載,該重載返回對象的引用。所以&**this成爲返回對象的地址。

換句話說,**this(*this->_Myptr)相同,並且&**this&(*this->_Myptr)相同。