#include "stdafx.h"
#include
using namespace std;
class Array {
int *ptr;
int size;
public:
Array(int size = 1) :size(size) {
cout << "Constructor DEFAULT" << endl;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = i + 1;
}
Array(const Array& obj) {
cout << "Constructor COPPY" << endl;
size = obj.size;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = obj.ptr;
}
Array(Array&& obj) {
cout << "Constructor REMOVE" << endl;
size = obj.size;
ptr = obj.ptr;
obj.ptr = nullptr;
obj.size = 0;
}
~Array() {
cout << "Distructor" << endl;
if (ptr != nullptr)
delete[]ptr;
}
Array& operator=(const Array& obj) {
cout << "Operator =" << endl;
if (this == &obj)
return *this;
if (ptr != nullptr)
delete[]ptr;
size = obj.size;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = obj.ptr;
return *this;
}
Array& operator=(Array&& obj) {
cout << "Operator REMOVE" << endl;
if (this == &obj)
return *this;
for (int i = 0; i < size; i++)
cout << ptr << " - ";
cout << endl;
if (ptr != nullptr)
delete[]ptr; //-------------------------------------------------------Здесь отладчик показывает ошибку
size = obj.size;
ptr = obj.ptr;
obj.ptr = nullptr;
obj.size = 0;
return *this;
}
Array operator+(const Array& obj) {
Array tmp = *this;
int *ptr_new = new int(size + obj.size);
for (int i = 0; i < size; i++)
ptr_new = ptr;
for (int i = size; i < size + obj.size; i++)
ptr_new = obj.ptr[i - size];
if (ptr != nullptr)
delete[]tmp.ptr;
tmp.ptr = ptr_new;
tmp.size = size + obj.size;
cout << tmp.ptr[6] << endl;
cout << "PLUS OPERATOR" << endl;
return tmp;
}
void show() {
for (int i = 0; i < size; i++)
cout << ptr << " ";
cout << endl;
}
friend ostream& operator<<(ostream& out, const Array& obj);
};
ostream& operator<<(ostream& out, const Array& obj) {
for (int i = 0; i < obj.size; i++)
cout << obj.ptr << " ";
cout << endl;
return out;
}
int main()
{
Array a1(3);
cout << "a1: " << a1;
Array a2(4);
cout << "a2: " << a2;
Array a3(1);
cout << "a3: " << a3;
a3 = a1 + a2;
cout << a3;
system("pause");
return 0;
}
#include
using namespace std;
class Array {
int *ptr;
int size;
public:
Array(int size = 1) :size(size) {
cout << "Constructor DEFAULT" << endl;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = i + 1;
}
Array(const Array& obj) {
cout << "Constructor COPPY" << endl;
size = obj.size;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = obj.ptr;
}
Array(Array&& obj) {
cout << "Constructor REMOVE" << endl;
size = obj.size;
ptr = obj.ptr;
obj.ptr = nullptr;
obj.size = 0;
}
~Array() {
cout << "Distructor" << endl;
if (ptr != nullptr)
delete[]ptr;
}
Array& operator=(const Array& obj) {
cout << "Operator =" << endl;
if (this == &obj)
return *this;
if (ptr != nullptr)
delete[]ptr;
size = obj.size;
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr = obj.ptr;
return *this;
}
Array& operator=(Array&& obj) {
cout << "Operator REMOVE" << endl;
if (this == &obj)
return *this;
for (int i = 0; i < size; i++)
cout << ptr << " - ";
cout << endl;
if (ptr != nullptr)
delete[]ptr; //-------------------------------------------------------Здесь отладчик показывает ошибку
size = obj.size;
ptr = obj.ptr;
obj.ptr = nullptr;
obj.size = 0;
return *this;
}
Array operator+(const Array& obj) {
Array tmp = *this;
int *ptr_new = new int(size + obj.size);
for (int i = 0; i < size; i++)
ptr_new = ptr;
for (int i = size; i < size + obj.size; i++)
ptr_new = obj.ptr[i - size];
if (ptr != nullptr)
delete[]tmp.ptr;
tmp.ptr = ptr_new;
tmp.size = size + obj.size;
cout << tmp.ptr[6] << endl;
cout << "PLUS OPERATOR" << endl;
return tmp;
}
void show() {
for (int i = 0; i < size; i++)
cout << ptr << " ";
cout << endl;
}
friend ostream& operator<<(ostream& out, const Array& obj);
};
ostream& operator<<(ostream& out, const Array& obj) {
for (int i = 0; i < obj.size; i++)
cout << obj.ptr << " ";
cout << endl;
return out;
}
int main()
{
Array a1(3);
cout << "a1: " << a1;
Array a2(4);
cout << "a2: " << a2;
Array a3(1);
cout << "a3: " << a3;
a3 = a1 + a2;
cout << a3;
system("pause");
return 0;
}