I only ask this question because I have isolated it to pretty much three lines of code, so it’s probably something really obvious that I just screwed up because I’m inexperienced:
Movie* ptr;
if(title == r->getItem().getTitle())
{
ptr = &(r->getItem());
cout << "Found: " << ptr->getTitle() << "!";
}
Essentially whats going on is that title == "The Godfather" so therefore that’s what
r->getItem().getTitle() is returning. What I don’t understand though, is that when I try to make a pointer that points to the Movie object returned by r.getItem(), I can’t use the pointer. It definitely had something assigned to it, but when I try to do ptr->getTitle() its returning null.
What on earth is going on?
I think you are doing something like this
The problem is that m is a stack object which gets lost after getItem has returned. That’s a common mistake in C++.
The getTitle call should not work either, but it might by chance.
You must create the object on the heap.
My simple rule to avoid this: Don’t use the & operator altogether unless you haven’t thought about what you are doing ten times.