Guest User

rvalue reference clumsy test

a guest
Feb 8th, 2012
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <iostream>
  4. #include <stdexcept>
  5. #include <utility>
  6.  
  7. class Little {
  8. public:
  9.  
  10.     int i;
  11.  
  12.     Little(): i(0) {
  13.         std::clog << "Little()" << std::endl;
  14.     }
  15.  
  16.     explicit Little(int i): i(i) {
  17.         std::clog << "Little(" << i << ')' << std::endl;
  18.     }
  19.  
  20.     Little(const Little &l): i(l.i) {
  21.         std::clog << "Little(const Little &)" << std::endl;
  22.  
  23.         if (this == &l)
  24.             throw std::invalid_argument("self assignment");
  25.     }
  26.  
  27.     Little & operator = (const Little &l) {
  28.         std::clog << "Little & operator = (const Little &)" << std::endl;
  29.  
  30.         if (this == &l)
  31.             throw std::invalid_argument("self assignment");
  32.  
  33.         i = l.i;
  34.         return *this;
  35.     }
  36.  
  37.     ~Little() {
  38.         std::clog << "~Little()" << std::endl;
  39.     }
  40. };
  41.  
  42. class Big {
  43. private:
  44.  
  45.     Little *pl;
  46.     std::size_t s;
  47.  
  48. public:
  49.  
  50.     explicit Big(std::size_t s): s(s) {
  51.         std::clog << "Big(" << s << ')' << std::endl;
  52.         pl = new Little[s];
  53.         std::fill_n(pl, s, Little());
  54.     }
  55.  
  56.     Big(std::size_t s, int i): s(s) {
  57.         std::clog << "Big(" << s << ", " << i << ')' << std::endl;
  58.         pl = new Little[s];
  59.         std::fill_n(pl, s, Little(i));
  60.     }
  61.  
  62.     Big(const Big &b): s(b.s) {
  63.         std::clog << "Big(const Big &)" << std::endl;
  64.  
  65.         if (this == &b)
  66.             throw std::invalid_argument("self assignment");
  67.  
  68.         if (b.pl == nullptr)
  69.             throw std::invalid_argument("nullptr pl");
  70.  
  71.         pl = new Little[s];
  72.         std::copy(b.pl, b.pl + b.s, pl);
  73.     }
  74.  
  75.     Big(Big &&b): s(b.s), pl(b.pl) {
  76.         std::clog << "Big(Big &&)" << std::endl;
  77.  
  78.         if (b.pl == nullptr)
  79.             throw std::invalid_argument("nullptr pl");
  80.  
  81.         b.s = 0;
  82.         b.pl = nullptr;
  83.     }
  84.  
  85.     Big & operator = (const Big &b) {
  86.         std::clog << "Big & operator = (const Big &)" << std::endl;
  87.  
  88.         if (this == &b)
  89.             throw std::invalid_argument("self assignment");
  90.  
  91.         if (b.pl == nullptr)
  92.             throw std::invalid_argument("nullptr pl");
  93.  
  94.         s = b.s;
  95.         delete[] pl;
  96.         pl = new Little[s];
  97.         std::copy(b.pl, b.pl + b.s, pl);
  98.         return *this;
  99.     }
  100.  
  101.     Big & operator = (Big &&b) {
  102.         std::clog << "Big & operator = (Big &&)" << std::endl;
  103.  
  104.         if (b.pl == nullptr)
  105.             throw std::invalid_argument("nullptr pl");
  106.  
  107.         std::swap(s, b.s);
  108.         std::swap(pl, b.pl);
  109.         return *this;
  110.     }
  111.  
  112.     void info() {
  113.         std::clog << "void info()" << std::endl;
  114.         std::cout << "---------------------------------------\n";
  115.         std::cout << "Big @ " << this << " information\n";
  116.         std::cout << "---------------------------------------\n";
  117.         std::cout << "s:\t\t" << s << '\n';
  118.         std::cout << "(*pl).i:\t" << (*pl).i << '\n';
  119.         std::cout << "pl:\t\t" << pl << '\n';
  120.         std::cout << "---------------------------------------" << std::endl;
  121.     }
  122.  
  123.     void setpl(int i) {
  124.         std::clog << "void setpl(" << i << ')' << std::endl;
  125.         std::fill_n(pl, s, Little(i));
  126.     }
  127.  
  128.     ~Big() {
  129.         std::clog << "~Big()" << std::endl;
  130.         delete[] pl;
  131.     }
  132. };
  133.  
  134. Big createBig() {
  135.     std::clog << "Big createBig()" << std::endl;
  136.     return Big(2, 13);
  137. }
  138.  
  139. int main() {
  140.     Big b(std::move(createBig()));
  141.     b.info();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment