[英]How to call a member function of an object using that object's pointer?
I have a Node object that has a public member function. 我有一个具有公共成员函数的Node对象。 When I have a pointer (or double pointer) in this case pointing to the original object, how do I call the member function? 在这种情况下,如果我有一个指针(或双指针)指向原始对象,该如何调用成员函数?
Here is the member function in the Node class: 这是Node类中的成员函数:
class Node {
public:
...
int setMarked();
...
private:
...
int marked;
...
};
And here is where I am trying to call that function: 这是我尝试调用该函数的地方:
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.
And just in case it matters, the .setMarked() function looks like this: 为了防万一,.setMarked()函数如下所示:
int Node::setMarked() {
marked = 1;
return marked;
}
Dereference it twice first. 首先取消引用两次。 Note that * binds less tightly than .
请注意,*的绑定不如紧密.
or ->
, so you need parens: 或->
,因此您需要括号:
(**s).setMarked();
Or, 要么,
(*s)->setMarked();
In your original code, the compiler was seeing the equivalent of 在您的原始代码中,编译器看到了相当于
**(s.setMarked());
which is why it wasn't working. 这就是为什么它不起作用的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.