Hello, I am writing a class for working with sets. I use an overload of an assignment operator and a copy, as well as an overload of an assignment and a copy with "transfer". Ie only 4 overload. I did a program tracing, but I could not figure out at what point the copy statement or the copy transfer statement with "transfer" occurs (how the program decides what to use) and the like. Not everything is so simple with these overloads, I hope for a detailed answer.
#include "stdafx.h" #include <iostream> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; class Set { static int len; //Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΡ
ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ² int n; //ΠΠΎΡΠ½ΠΎΡΡΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° static bool mode; //0 ΠΎΠ±ΡΡΠ½ΡΠΉ ΡΠ΅ΠΆΠΈΠΌ, 1 ΡΠ΅ΠΆΠΈΠΌ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ static int U; //ΠΠΎΡΠ½ΠΎΡΡΡ ΡΠ½ΠΈΠ²Π΅ΡΡΡΠΌΠ° char litera; //ΠΠ±ΠΎΠ·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° A,B,C,D... char* universe; //Π£ΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ public: Set(char _litera); ~Set(); Set(); void print(); int power() { return n; } //ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π°Π΄ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π°ΠΌΠΈ Set operator & (const Set& B) const; Set operator ~ () const; //ΠΠ΅ΡΠ΅Π³ΡΡΠ·ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠΎΠ² ΠΊΠΎΠΏΠΈΠΈ ΠΈ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ Set(const Set &); Set operator = (const Set&); //ΠΠ΅ΡΠ΅Π³ΡΡΠ·ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠΎΠ² ΠΊΠΎΠΏΠΈΠΈ ΠΈ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΎΠΌ Set(Set &&); Set operator = (Set &&); }; Set::Set() : litera('D' + (len++)), n(0), universe(new char[U + 1]) { if (mode) cout << "Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << " = []\n"; universe[0] = '\0'; }; Set ::~Set() { if(mode) cout << "Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << '\n'; delete[] universe; } Set::Set(char _litera) : litera(_litera), n(0), universe(new char[U + 1]) { for (int i = 0; i < U; i++) { if (rand() % 2) { universe[n++] = '0' + i; } } universe[n] = '\0'; cout << "Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << " = [" << universe << "]\n"; } void Set::print() { cout << litera << " = [" << universe << "]" << '\n'; } //ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π°Π΄ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π°ΠΌΠΈ Set Set:: operator & (const Set& B) const { Set C; for (int i = 0; i < n; i++) { for (int j = 0; j < Bn; j++) { if (universe[i] == B.universe[j]) C.universe[C.n++] = universe[i]; } } C.universe[Cn] = '\0'; if(mode) cout << "Π Π΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅ΡΠ΅ΡΠ΅Π½ΠΈΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << " = [" << universe << "]" << " ΡΠΎ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎΠΌ " << B.litera << " = [" << B.universe << "]" << " => " << C.litera << " = [" << C.universe << "]\n"; return C; } Set Set:: operator ~ () const { Set C; for (char ch = '0'; ch <= '9'; ch++) { bool flag = true; for (int j = 0; j < n; j++) { if (ch == universe[j]) { flag = false; break; } } if (flag) C.universe[C.n++] = ch; } C.universe[Cn] = '\0'; if(mode) cout << "Π Π΅Π·ΡΠ»ΡΡΠ°Ρ ΠΈΠ½Π²Π΅ΡΡΠΈΠΈ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << " = [" << universe << "]" << " => " << C.litera << " = [" << C.universe << "]\n"; return C; } //ΠΠ΅ΡΠ΅Π³ΡΡΠ·ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΎΠ² ΠΊΠΎΠΏΠΈΠΈ ΠΈ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ Set::Set(const Set & B) : n(Bn), litera(B.litera), universe(new char[U + 1]) { if (mode) cout << "ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << B.litera << '\n'; memcpy(universe, B.universe, U + 1); } Set Set :: operator = (const Set& B) { if(mode) cout << "ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ " << litera << " = " << B.litera << '\n'; if (this != &B) { n = Bn; memcpy(universe, B.universe, U + 1); litera = 'R'; } return *this; } //ΠΠ΅ΡΠ΅Π³ΡΡΠ·ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΎΠ² ΠΊΠΎΠΏΠΈΠΈ ΠΈ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΎΠΌ Set::Set(Set && B) : litera('D'+(len++)),n(Bn), universe(B.universe){ if (mode) cout << "ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << litera << " ΠΈΠ· ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° " << B.litera << " Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΎΠΌ\n"; B.universe = NULL; } Set Set:: operator = (Set&& B) { if(mode) cout << "ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ c ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΎΠΌ " << litera << " = " << B.litera << '\n'; if (this != &B) { n = Bn; universe = B.universe; litera = 'R'; B.universe = NULL; } return *this; } const unsigned int N = 1; int Set::U = 10; //ΠΠΎΡΠ½ΠΎΡΡΡ ΡΠ½ΠΈΠ²Π΅ΡΡΡΠΌΠ° Π΄Π»Ρ Π΄Π΅ΡΡΡΠΈΡΠ½ΡΡ
ΡΠΈΡΡ bool Set::mode = 1; //0 ΠΎΠ±ΡΡΠ½ΡΠΉ ΡΠ΅ΠΆΠΈΠΌ, 1 ΡΠ΅ΠΆΠΈΠΌ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ int Set::len = 1; //Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΡ
ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²; int main() { setlocale(LC_ALL, ""); srand(time(NULL)); time_t start, stop; { Set A('A'), B('B'), C('C'), D('D'), E; system("pause"); //ΠΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ² start = clock(); for (int i = 0; i < N; i++) { E = ((A & ~(B & C)) & ~D); } stop = clock(); //ΠΡΠ²ΠΎΠ΄ Π²ΡΠ΅Ρ
ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ² A.print(); B.print(); C.print(); D.print(); E.print(); //ΠΡΠ²ΠΎΠ΄ ΡΡΠ΅Π΄Π½Π΅ΠΉ ΠΌΠΎΡΠ½ΠΎΡΡΠΈ ΠΈ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ cout << "Average power = " << (A.power() + B.power() + C.power() + D.power() + E.power()) / 5 << '\n'; cout << "Time = " << stop - start << '/' << N << '\n'; } system("pause"); return 0; } 