quodlibetor
quodlibetor

Reputation: 8433

How to dereference a pointer passed by reference in c++?

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.

Upvotes: 1

Views: 3068

Answers (3)

Darryl
Darryl

Reputation: 6217

there should be a way to dereference a pointer reference to get a new pointer that points to the same object.

Well, how about this?

Node* n = node;

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.

Upvotes: 4

rmeador
rmeador

Reputation: 25696

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 to instantiateNode()? It will always be NULL, so what good does it do? If instantiateNode() also has its first argument passed as a reference, why? You are passing in the parent, so instantiateNode() can just access the thing node should represent via parent->child. Even if for some reason instantiateNode() 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 from insert()'s first argument. Whatever instantiateNode() 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:

insert(node, val, node->child);

Upvotes: 0

Graeme Perrow
Graeme Perrow

Reputation: 57248

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?

Upvotes: 3

Related Questions