[英]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.