/* simple class that has a vector of ints within it */
class A
{
public:
vector<int> int_list;
};
/* some function that just returns an int, defined elsewhere */
int foo();
/* I want to fill the object's int_list up */
A a_obj;
int main() {
for (int i = 0; i < 10; i++) {
int num = foo();
a_obj.int_list.push_back( num );
}
}
Is the scope of num limited to the for loop? Will it be destroyed once the for loop is exited? If I try to access the numbers in a_obj‘s int_list will I not be able to as the numbers inside will have been destroyed?
Yes
It’ll be destroyed after each iteration, and then created again for the next!
Containers store copies, so you don’t have to worry about this.
You only run into these sorts of issues with references and pointers.