2016-12-31 52 views
0

例如,我有此類:如何知道方法中的對象地址?

class example{ 
public: 
     int beauty; 
     void CompareObject(const example& another_object, example*& ptr); 
}; 

方法CompareObject()該對象與對象another_object進行比較,(通過引用),並保存在指針PTR最美麗的對象的地址(也由傳遞參考)

的問題是在CompareExampleObject:

void CompareExampleObject (const example& another_object, example*& ptr){ 
    // set the best object 
    if(beauty < another_object.beauty) 
     ptr = &another_object; 
    else 
     ptr = // !!! What should I write here? 
} 

回答

4

this是對象的一種方法內的對象的地址。

+0

非常感謝。我很難過問了一個這麼簡單的問題,但我仍然是C++的新手...... –

+0

如果你發現答案有用,一旦你有15個代表,你可以點擊答案左上角的箭頭, upvote它。您也可以通過點擊左邊空白處的灰色刻度標記來「接受」答案 - 您應該稍微等一下,看看是否有更好的答案。 –

相關問題