fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Car;
  7.  
  8. class Garage {
  9. public:
  10. vector<Car> vCar;
  11.  
  12. };
  13.  
  14. class Car {
  15. public:
  16. short id;
  17. };
  18.  
  19. int main()
  20. {
  21. Garage garage;
  22. short i;
  23.  
  24. for(i = 0; i < 10; i++) {
  25. Car car;
  26. car.id = i;
  27.  
  28. garage.vCar.push_back(car);
  29. }
  30.  
  31. for(i = 0; i < garage.vCar.size(); i++) {
  32. cout << i << " " << garage.vCar[i].id << endl;
  33. // output 9,9,9,..9 instead of 1,2,3,4...10, why is that?
  34. }
  35.  
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9