#include "Array.h" Array::Array(int n) { size = n; array = new int[n]; } Array::Array() { size = 0; array = 0; } void Array::input() { for (int i = 0; i < size; i++) cin >> array[i]; } void Array::show() { for (int i = 0; i < size; i++) cout << array[i] << " "; } Array Array::connect(Array array2) { bool label = false; int count = 0; for (int i = 0; i < size; i++) { label = false; for (int j = 0; j < array2.size; j++) { if (array[i] == array2.array[j]) { label = true; break; } } if (label == false) count++; } Array array3(size + count); count = size; for (int i = 0; i < size; i++) array3.array[i] = array[i]; for (int i = 0; i < array2.size; i++) { label = false; for (int j = 0; j < size; j++) { if (array2.array[i] == array3.array[j]) { label = true; break; } } if (label == false) { array3.array[count] = array2.array[i]; count++; } } return array3; } Array::~Array() { delete[] array; } #include "Array.h" int main() { int n; cout << "Input Array_1 size: "; cin >> n; Array array1(n); cout << "Input Array_1 data: "; array1.input(); cout << "Input Array_2 size: "; cin >> n; Array array2(n); cout << "Input Array_2 data: "; array2.input(); Array array3 = array1.connect(array2); cout << "Result: " << endl; array1.show(); cout << endl; array2.show(); cout << endl; array3.show(); cout << endl; array1.~Array(); array2.~Array(); array3.~Array(); return 0; } 

Why is the data in objects normal before the connect function and distorted after it? Is it a fun pointers game?

  • It is also obvious that the code does not comply with the Rule of Three . And it is critical for example in the function connect , which returns an object by value. - AnT September
  • There is an initialization, the code is badly copied. I don’t know about the rule of three, I just began to comprehend OOP) - Nikita

2 answers 2

If you are in your class doing manual resource management (for example, dynamic memory), then you should also follow the Rule of Three: do not forget to implement the correct copy constructor , copy assignment operator, and destructor .

(In modern C ++, this rule has been transformed into the Rule of Five, that is, a moving constructor and a moving assignment operator have been added. But this is optional.)

In your particular case, due to the lack of a correctly implemented copy constructor, neither the correct transfer of the parameter to the connect method, nor the correct return of the result from this method is possible.

(Why the transfer of a parameter to the connect method is done by value is a separate question.)

It is also worth noting that it is not clear why the explicit calls made by your objects at the end of the main function will lead to undefined behavior later, when the same destructors will be called again automatically.

     Array::Array(int n) { array = new int[n]; } 

    why in this constructor size is not initialized? Everything will already work incorrectly.

     for (int i = 0; i < size; i++) array3.array[i] = array[i]; 

    there may be different sizes of arrays, and below the code too.

    • The connect function algorithm is correct, there are no errors in it. The whole point is that the function does not return the array3 that we synthesize during the function. and changes in some way array2, although we do not change anything in the function in it. - Nikita