Please note my homework tag. As with all homework, helpful suggestions over straight answers to actual coding is appreciated. Feel free to answer any of my conceptual questions straight forwardly, though.
Hello,
My professor assigned us a doubly linked list for homework, and I was avoiding asking for help until I absolutely needed it, and here I am.
He provides us header files, which we then have to make a class for, and must follow the header file perfectly. The way he does his copy constructor is that he makes us write a helper function that we just have the copy constructor call.
I can do this easily, on a normal case, but this time he has given us a very bizarre signature for the helper function:
// copys chain at oldHead to newHead.
static void copy(Elem *&newHead, const Elem *oldHead)
This is to copy a chain of structs called Elems:
struct Elem
{
Information info;
Elem *next;
Elem *back;
};
I guess I’m mostly confused as to what the whole Elem *& business because, from what I remember, don’t & and * cancel each other out?
Thanks, any and all help is really appreciated! Hopefully this will help other people in my position in the future:)
Could have been a potential function. You take the old Head and return the new cloned Head.
What he chose is passing the pointer by reference.
If it was simply
something like above. Any changes to newHead is not visible to outside the function.
This is the same below. x is passed by value. Any changes to x are forgotten after the function Addten returns. Your x just happens to be a pointer.