struct myclass {
int id;
myclass(): id(-1){};
};
myclass *a;
cout >> a->id;
This above is a demonstration of my long program. The output should be -1. But I do not know why the output becomes -842150451.
struct myclass {
int id;
myclass(): id(-1){};
};
myclass *a;
cout >> a->id;
This above is a demonstration of my long program. The output should be -1. But I do not know why the output becomes -842150451.
You have Undefined Behavior!
You create a myclass
pointer, but never point it to anything. And then you try to dereference the dangling or wild pointer.
Try this instead to get the -1 you're expecting:
myclass a;
std::cout << a.id;