fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. using namespace std;
  6.  
  7. class Entity {
  8. public:
  9.  
  10. int index;
  11. Entity() {};
  12. virtual ~Entity() {};
  13. virtual void hit() = 0;
  14. };
  15. class Mesh : public Entity {
  16.  
  17. public:
  18. //int index; // No don't redefine: here you'd have two different indexes
  19. Mesh(int x) {this->index=x;};
  20. void hit() override {} // extraa safety: use override instead of virtual to be sure to override
  21. };
  22.  
  23. int main() {
  24. vector<unique_ptr<Entity>> objects;
  25. objects.push_back(unique_ptr<Entity>(new Mesh(35)));
  26. objects.push_back(unique_ptr<Entity>(new Mesh(10)));
  27.  
  28. for ( int i = 0 ; i < objects.size() ; i++ )
  29. cout << objects[i]->index << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5444KB
stdin
Standard input is empty
stdout
35
10