I encountered a very strange thing today. I found that the following code will trigger a breakpoint, maybe because the memory of the class object "b" I created in qqq() is released, and the pointer will be invalid.
#include<iostream>
#include<vector>
#include"ww.h"//A header file for a class I defined, which has a class called apple in it
using namespace std;
vector<apple*>arr_apple_p;
void qqq(apple* &);
int main()
{
apple* p_apple=NULL;
qqq(p_apple);
arr_apple_p[0]->print();
}
void qqq(apple* &a)
{
apple b;
a = &b;
arr_apple_p.push_back(a);
}
However, if I change the code
arr_apple_p[0]->print();
to
p_apple->print();
It will work fine! I'm wondering why this is?