Edit: my question was relating to the difference between compilers but I was just mislead to think there was different behavior when really both compilers were showing the expected "undefined behavior"
I know that using the static keyword here would be good for the building_num integer, but I don't understand why the value prints correctly when compiling with g++, I would think the integer value should be erased after the assign_building_num function terminates
With visual studio's compiler I see the behavior I assume, where it doesn't print out 400, it prints out some random number (in this case it is always 32759 for some reason)
#include <iostream>
#include <string>
using namespace std;
class building {
int* ptr_building_num;
public:
void set_building_num(int* num_ptr) {
ptr_building_num = num_ptr;
}
void print_building() {
cout << *ptr_building_num << flush;
}
};
void assign_building_num(building* building_ptr) {
//int* ptr_to_building_num = nullptr;
int building_num = 400;
int* ptr_to_building_num = &building_num;
building_ptr->set_building_num(ptr_to_building_num);
}
int main() {
building* ptr_to_some_building = nullptr;
ptr_to_some_building = new building;
assign_building_num(ptr_to_some_building);
ptr_to_some_building->print_building();
return 0;
}