Guest User

rvalue ref #3

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