leproza

Extract data

Jan 3rd, 2023
1,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base
  6. {
  7. public:
  8.   virtual void init() = 0;
  9. };
  10.  
  11. class Impl : public Base
  12. {
  13.   char m_data[ 2 ];
  14. public:
  15.   virtual void init()
  16.   {
  17.     m_data[ 0 ] = 'c';
  18.     m_data[ 1 ] = 'x';
  19.   }
  20. };
  21.  
  22. Base* getInstance()
  23. {
  24.   Base* res = new Impl();
  25.   res->init();
  26.   return res;
  27. }
  28.  
  29. struct Extractor
  30. {
  31.   char data[ 10 ];
  32. };
  33.  
  34. int main()
  35. {
  36.   Base* b = getInstance();
  37.   Extractor* ext = (Extractor*)( b );
  38.   cout << "1 byte " << ext->data[ sizeof( void* ) +0 ] << endl;
  39.   cout << "2 byte " << ext->data[ sizeof( void* ) +1 ] << endl;
  40.   return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment