fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Product
  5. {
  6. public:
  7. int ID;
  8. std::string Designation;
  9. };
  10.  
  11. struct Property
  12. {
  13. std::string Name;
  14. int Shift;
  15. };
  16.  
  17. class ProductSchema
  18. {
  19. private:
  20. ProductSchema();
  21. public:
  22. static ProductSchema& Default();
  23. ProductSchema(const ProductSchema& other) = delete;
  24. Property ID;
  25. Property Designation;
  26. Property Prix;
  27. };
  28. ProductSchema::ProductSchema()
  29. {
  30. ID.Name = "ID";
  31. ID.Shift = offsetof(Product, ID);
  32.  
  33. Designation.Name = "Designation";
  34. Designation.Shift = offsetof(Product, Designation);
  35. }
  36.  
  37. ProductSchema& ProductSchema::Default()
  38. {
  39. static ProductSchema instance_;
  40. return instance_;
  41. }
  42.  
  43. int main()
  44. {
  45. Product p;
  46. p.ID = 12345;
  47. cout << "Before: " << p.ID << endl;
  48. int* pID = reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(&p) + ProductSchema::Default().ID.Shift);
  49. *pID = 67890;
  50. cout << "After: " << p.ID << endl;
  51. }
Success #stdin #stdout 0.01s 5472KB
stdin
Standard input is empty
stdout
Before: 12345
After: 67890