Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstddef>
- #include <iostream>
- #include <stdexcept>
- #include <utility>
- 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_n(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_n(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 << '\n';
- std::cout << "---------------------------------------" << std::endl;
- }
- void setpl(int i) {
- std::clog << "void setpl(" << i << ')' << std::endl;
- std::fill_n(pl, s, Little(i));
- }
- ~Big() {
- std::clog << "~Big()" << std::endl;
- delete[] pl;
- }
- };
- Big createBig() {
- std::clog << "Big createBig()" << std::endl;
- return Big(2, 13);
- }
- int main() {
- Big b(std::move(createBig()));
- b.info();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment