如何在 c++ 中取消引用通过引用传递的指针?
我正在做(类似的事情):
void insert(Node*& node, string val, Node* parent)
{
if (node == NULL)
instantiateNode(node, val, parent);
else
insert(node->child, val, node);
}
事情是, instantiateNode(...,parent)
似乎修改了传递到函数中的原始 *&node
设置*parent
时。 instantiateNode()
应该来改变节点
,但是如果它改变父
,那么你就有一个节点设置为其父级,这没有意义,也不起作用。完全没有。
我之所以烦恼指针引用,是因为它消除了特殊情况,并显着减少了我必须执行的错误检查量。由于我这样做是为了减少行数和琐碎的算法重复,因此我可以通过将代码行数大约加倍来解决这个问题。但我宁愿不这样做,而且我觉得应该有一种方法来取消对指针引用的引用,以获取指向同一对象的新指针。而且,实际上,我认为通过 *parent
传递 *&node
应该可以做到这一点,但显然 gcc 正在对其进行优化。
I'm doing (something like) this:
void insert(Node*& node, string val, Node* parent)
{
if (node == NULL)
instantiateNode(node, val, parent);
else
insert(node->child, val, node);
}
The thing is, that instantiateNode(..., parent)
seems to modify the original *&node
passed into the function when setting the *parent
. instantiateNode()
is supposed to alter the node
, but if it alters the parent
than you have a node that is set to its parent, which doesn't make sense, and also doesn't work. At all.
The reason I'm bothering with pointer references at all is because it eliminates special cases and significantly reduces the amount of error checking I have to do. Since I'm doing it to reduce line count and trivial algorithm-ish duplication, I can get around this by approximately doubling the code line count. But I'd rather not, and I feel like there should be a way to dereference a pointer reference to get a new pointer that points to the same object. And, really, I thought that pass of *&node
through *parent
should have done it, but apparently gcc is optimizing it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这个怎么样?
现在您已经有了一个非引用指针,它指向与节点相同的对象,这正是您所要求的。
我 95% 确信您面临的问题与引用无关,而与 instantiateNode 中的错误逻辑或您对它的使用有关。这就是为什么如果您向我们提供有关代码应该执行的操作的更多信息,或者甚至将代码发布到 instantiateNode,将会很有用。
Well, how about this?
Now you've got a non-reference pointer that points to the same object as node, exactly what you asked for.
I am 95% sure the problem you are facing has nothing to do with references, and everything to do with faulty logic in instantiateNode or your use of it. That's why it would be useful if you gave us more information about what the code is supposed to do, or even posted the code to instantiateNode.
node是一个指针的引用,这意味着如果函数设置node,传入的值就会改变。这就是参考文献的工作原理。
如果您不希望节点在调用函数中发生更改,请不要将其设为引用。
或者我误解了什么?
node is a reference to a pointer, which means that if the function sets node, the value passed in is changed. That's how references work.
If you don't want node to change in the calling function, don't make it a reference.
Or have I misunderstood something?
我认为
node
不需要通过引用传递。一个简单的指针就可以完成这项工作。这是因为您不需要直接更改它。为什么要将它传递给instantiateNode()?它永远是NULL,那么它有什么用处呢?如果instantiateNode()
也将其第一个参数作为引用传递,为什么?您正在传递父级,因此instantiateNode()
可以通过parent->child
访问node
应该表示的事物。即使由于某种原因instantiateNode()
确实通过引用获取了它的参数并且需要这样做,我相信如果您从insert()
中删除引用,该函数仍然可以工作> 的第一个参数。当函数返回时,instantiateNode()
对该值所做的任何操作都将丢失。抱歉,这太令人困惑了,但我想不出更好的表达方式。对你的问题还有另一种解释......也许你对
insert()
的调用应该是:I don't think
node
needs to be passed by reference. A simple pointer should do the job. This is because you don't ever need to change it directly. Why are you passing it toinstantiateNode()
? It will always be NULL, so what good does it do? IfinstantiateNode()
also has its first argument passed as a reference, why? You are passing in the parent, soinstantiateNode()
can just access the thingnode
should represent viaparent->child
. Even if for some reasoninstantiateNode()
does take it's param by reference and it's required to do so, I believe the function will still work if you drop the reference frominsert()
's first argument. WhateverinstantiateNode()
does with that value will simply be lost when the function returns. I'm sorry that's so confusing, but I can't think of a better way to word it.There's another interpretation of your problem... maybe your call to
insert()
should read: