I am somewhat confused by why this code doesn't maintain the previously assigned value. I assume the pointer is not correctly set in the struct, but why/how? Any explanation or suggestion would be greatly appreciated! Thanks so much!
container.test is never initialized. If you think line 48 does this, you're incorrect. You're still passing by value (where you make a copy), it is just in this case you're passing a copy of the memory location of container.test (which is garbage). You immediately overwrite the copy on line 48, but you never do anything to get this new reference back into 'container'.
I made some changes from line 33 down:
Yes, I guess that is exactly what I falsely assumed - or hoped would happen. Clearly container.test remains un-initialized. How would I then pass the pointer correctly to myFunction (which is what I had actually intended)?
Thanks!
Edit: Oh, thanks so much for the solution!! I thought it's either a pointer or by reference, so it didn't occur to me to pass the Test* by reference &.
int myFunction(int num, Cnt& contain)
Sorry for the noob follow-up question, but is there a way to pass only the pointer (Test* test) by reference instead of the whole Cnt struct contain?