spikeysnack

ArrayStruct

Jun 30th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //wrap array in struct
  4. struct INT100
  5. {
  6.   int val[100] = {0};
  7. };
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11.   INT100 x;
  12.   // populate it
  13.   for ( auto& i : x.val)
  14.     {
  15.       static int last = 0;
  16.       i =  ++last;
  17.     }
  18.  
  19.   for ( auto i : x.val) std::cout << i << "\t";
  20.   std::cout << std::endl;
  21.  
  22.   // copy constructor
  23.   INT100 y(x); // copies data
  24.  
  25.   for ( auto i : y.val) std::cout << i << "\t";
  26.   std::cout << std::endl;
  27.  
  28.   std::cout << "sizeof(INT100):\t" <<   sizeof(INT100) << " bytes" << "\n";  
  29.   std::cout << "sizeof(x):\t" <<   sizeof(x) << "\n";  
  30.  
  31.   // copy initialization
  32.   auto z = new INT100{x};
  33.  
  34.   for ( auto i : z->val) std::cout << i << "\t";
  35.   std::cout << std::endl;
  36.  
  37.   delete z;
  38.   return 0;
  39. }
Add Comment
Please, Sign In to add comment