class Foo {
int num;
};
class Bar {
public:
Bar() {
Foo f1;
}
};
int main() {
Bar* b = new Bar();
return 0;
}
In the code above, I create an instance of Bar (b) on the heap. In the constructor of Bar, an instance of Foo (f1) is created. Since b is created on the heap, and f1 is temporary, I'm not sure - where is it stored when the constructor gets called?
Objects memory is handled automatically when stored on the stack, so if f1 is stored on the heap, how is it destructed automatically when the constructor of Bar is finished? On the other hand, if it is stored on the stack, how does the object b on the heap points to it?