Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // luxoft quiz application
  4. //
  5. // Created by Yaroslav Fedorov on 24/02/2017.
  6. // Copyright © 2017 Yaroslav Fedorov. All rights reserved.
  7. //
  8.  
  9. //
  10. // LUXOFT task for C++ Russia 2017 contest
  11. //
  12.  
  13. // Как получить информацию из закрытого члена класса? Решение должно быть
  14. // платформонезависмым и компиляторонезависисмым (нет информации о том, как данные
  15. // представленны в памяти)
  16.  
  17.  
  18. #include <iostream>
  19.  
  20. class TopSecret
  21. {
  22. public:
  23. TopSecret() : ultimateAnswerToLifeUniverseAndEverything(42) {}
  24. private:
  25. enum { NO, BYTE, MANIPULATION } nativeProtection;
  26. std::string aroundTopSecretInformation;
  27. int ultimateAnswerToLifeUniverseAndEverything;
  28. double footer;
  29. char terminator;
  30. };
  31.  
  32. int getValue(const TopSecret& t)
  33. {
  34. int answer;
  35.  
  36. //#1 answer
  37. struct TopSecretPrototype { enum {} nb; std::string s; int SecretValue; };
  38. answer = reinterpret_cast<const TopSecretPrototype *>( &t )->SecretValue;
  39.  
  40.  
  41. //#2 answer
  42. char *p = (char*)&t;
  43. enum X {};
  44. answer = int(*(p + sizeof(enum X) + sizeof(std::string) + sizeof(int)));
  45.  
  46. //#3 answer
  47. char *d = (char*)&t;
  48. auto alignedChar = sizeof(char) + 3;
  49. auto offset = sizeof(TopSecret) - sizeof(int *) - alignedChar - sizeof(double) - sizeof(int);
  50. answer = int(*(d + offset));
  51.  
  52. return answer;
  53. }
  54.  
  55.  
  56. int main(int argc, const char * argv[]) {
  57. std::cout <<
  58. "Answer to the Ultimate Question of "
  59. "Life, "
  60. "the Universe, "
  61. "and Everething is "
  62. << getValue( TopSecret() ) << std::endl;
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement