fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Vector3d { int x = 0; int y = 0; int z = 0;};
  5. class Position:public Vector3d {};
  6. class Velocity:public Vector3d {};
  7.  
  8. class Trajectory:public Position, public Velocity
  9. {
  10. public:
  11. Vector3d &GetPosition() { return static_cast<Position&>(*this); }
  12. Vector3d &GetVelocity() { return static_cast<Velocity&>(*this); }
  13. };
  14.  
  15. std::ostream& operator << (std::ostream& os, const Vector3d & v)
  16. {
  17. return os << '{' << v.x << ',' << v.y << ',' << v.z << ',' << '}';
  18. }
  19.  
  20.  
  21. int main()
  22. {
  23. Trajectory t;
  24.  
  25. t.GetPosition() = {1, 2, 3};
  26. t.GetVelocity() = {4, 5, 6};
  27.  
  28. std::cout << t.GetPosition() << t.GetVelocity() << std::endl;
  29. }
  30.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
{1,2,3,}{4,5,6,}