[英]How to call a member function of an object using that object's pointer?
我有一個具有公共成員函數的Node對象。 在這種情況下,如果我有一個指針(或雙指針)指向原始對象,該如何調用成員函數?
這是Node類中的成員函數:
class Node {
public:
...
int setMarked();
...
private:
...
int marked;
...
};
這是我嘗試調用該函數的地方:
Node **s;
s = &startNode; //startNode is the original pointer to the Node I want to "mark"
q.push(**s); //this is a little unrelated, but showing that it does work to push the original object onto the queue.
**s.setMarked(); //This is where I am getting the error and where most of the question lies.
為了防萬一,.setMarked()函數如下所示:
int Node::setMarked() {
marked = 1;
return marked;
}
首先取消引用兩次。 請注意,*的綁定不如緊密.
或->
,因此您需要括號:
(**s).setMarked();
要么,
(*s)->setMarked();
在您的原始代碼中,編譯器看到了相當於
**(s.setMarked());
這就是為什么它不起作用的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.