fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. struct Element {
  5. int x;
  6. };
  7.  
  8. class Y {
  9. private:
  10. std::vector<Element*> elements;
  11. public:
  12. void addElement(Element& e) {
  13. elements.push_back(&e);
  14. }
  15. void printElements() {
  16. for(auto it = elements.begin(); it!=elements.end(); ++it) {
  17. std::cout << (*it)->x << std::endl;
  18. }
  19. }
  20. };
  21.  
  22. int main() {
  23. Element e = {0};
  24. Y y;
  25. y.addElement(e);
  26. y.printElements();
  27. e.x = 10;
  28. y.printElements();
  29. }
Success #stdin #stdout 0s 2960KB
stdin
Standard input is empty
stdout
0
10