This is some code I drew from the C++ Crash Course by Josh Lospinoso I'm using to teach myself c++.
Please correct me if I'm wrong, but it seems that an Element Object called "new_element" was created when he created a pointer called "new_element.
Where I'm mostly confused is at the line of code "new_element->next = next;"
What's really going on here? I know that the "next" pointer of trooper1 will be pointing to the "next" pointer (or is this the base address of trooper2 which happens to be where its "next" pointer lives) of trooper2. I just can't see how this is happening.
At initializing time next does not point to anything (= nullptr). Line 18 basically appends trooper2 after trooper1. trooper2->next points to whatever trooper1->next pointed to. In this case nullptr. trooper1->next will now point to trooper2.
Okay. So it's like the pointer of trooper2 picks up where the pointer of trooper1 left off, so that pointer of trooper1 can be released to point to trooper2?
Why does it have to be released? In other words why can't we eliminate line 5 and jump straight to line 6 especially if trooper1->next and trooper2->next both don't point to anything?
Since the arrow operator dereferencesnew_element and allows it access to the contents of trooper2 and enables it to edit those contents, we're able to say