通过参考(c++)传递对象
我想要一种从数组中创建二进制树而没有返回的方法。因此,我必须通过参考来工作,但是我在适当的语法中遇到了一些麻烦。
显然,我已经尝试在发布此问题之前尝试搜索,我已经看到了类似的帖子,但是没有答案确实有帮助。
到目前为止,我在这里所做的事情:
我的班级:
class Node
{
private:
int data;
Node* left;
Node* right;
public:
[...]
void make_tree(Node* node, int values[], int size);
};
方法:
// Every operations are done a copy of node, I want them to be done on the original object
void Node::make_tree(Node* node, int values[], int size)
{
if (size <= 0)
{
return;
}
else
{
if (!node)
{
node = new Node(values[size]);
}
else if (!node->has_data())
{
node->set_data(values[size]);
}
// recursive call
make_tree(node->left, values, size - 1);
make_tree(node->right, values, size - 1);
}
}
呼叫:
int main()
{
int tab[] = { 5,3,4,9,7,6 };
Node* root = new Node();
/* I want "root" to be passed by reference so every modifications
are done on the original object and not a copy who will be destroyed
at the end of the method scope. */
root->make_tree(root, tab, 6);
root->print_tree(); // there is nothing more in root
}
由于我已经将指针传递给对象“根”,所以我对如何做到这一点感到困惑。
谢谢。
PS:我知道我的递归电话没有做我所描述的事情。这是其他时间的问题。
pps:第一个帖子,因此,如果您看到我做错了什么,请告诉我。
I want a method that creates a binary tree from an array and returns nothing. So I would have to work by reference but I am having some troubles with the proper syntax to use.
I have obviously tried to search before posting this question, I have seen similar posts but no answer were truly helpfull.
Here what i have done so far :
My Class :
class Node
{
private:
int data;
Node* left;
Node* right;
public:
[...]
void make_tree(Node* node, int values[], int size);
};
The method :
// Every operations are done a copy of node, I want them to be done on the original object
void Node::make_tree(Node* node, int values[], int size)
{
if (size <= 0)
{
return;
}
else
{
if (!node)
{
node = new Node(values[size]);
}
else if (!node->has_data())
{
node->set_data(values[size]);
}
// recursive call
make_tree(node->left, values, size - 1);
make_tree(node->right, values, size - 1);
}
}
The call :
int main()
{
int tab[] = { 5,3,4,9,7,6 };
Node* root = new Node();
/* I want "root" to be passed by reference so every modifications
are done on the original object and not a copy who will be destroyed
at the end of the method scope. */
root->make_tree(root, tab, 6);
root->print_tree(); // there is nothing more in root
}
Since I am already passing a pointer to the object "root", I am confused on how I could do it.
Thank you.
PS: I am aware that my recursive call does not do what I described it should do. That is a problem for an other time.
PPS : first post btw, so if you see something I did wrong, please, tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
节点
指针在函数中无法在函数中进行修改,因为您按值传递(仅修改了副本)。您可以使用注释中建议的参考:
或者还可以使用指针指向指针
void node :: make_tree(node ** node ** node,int values [],int size);
修改您的代码将是更多的工作。Node
pointer cannot be modified inside the function, as you pass it by value (only the copy is modified).You can use a reference, as suggested in comments :
Or you can also use a pointer to a pointer
void Node::make_tree(Node** node, int values[], int size);
but there will be more work to modify your code.