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