I am new to C++ and experimenting with using pointers to return a values. I have written up a simple program, but I cannot understand why it crashes.
main.cpp
int main() {
bool *pMyBool;
bar myBar;
myBar.foo(pMyBool);
cout << *pMyBool << endl;
return 0;
}
bar.cpp
int bar::foo(bool *pMyBool) {
bool myBool = true;
*pMyBool = myBool;
return 0;
}
output
1
//then it crashes
In C++, you don’t want to use pointers in your case, but to use references:
Of course, you’ll just call from your
main