Help, please, deal with operator overloading. How to write an overload for an assignment operator (=) and an addition operator (+) to this code fragment. It is necessary that the commented construction works correctly.

#include <iostream> #include <cstdlib> #include <ctime> class V { private: int dimension; double* ptrdata = 0; public: V(int _dimension = 1): dimension(_dimension){} V(const V &obj) : dimension(obj.dimension){} ~V(){ delete [] ptrdata; } void Fill(); void Show() const; }; int main() { V obj1(10); obj1.Fill(); obj1.Show(); V obj2(10); obj2.Fill(); obj2.Show(); V obj3(10); // obj3 = obj1 + obj2; // obj3.Show(); return 0; } void V::Fill() { srand((unsigned)time(NULL)); ptrdata = new double [dimension]; for (int i = 0; i < dimension; i++) { ptrdata[i] = rand() % 10 + 1; } } void V::Show() const { for (int i = 0; i < dimension; i++) std::cout << ptrdata[i] << " "; std::cout << std::endl; } 
  • one
    And what is the semantics of addition in this case? What should it do at all? - Harry
  • Just add values ​​by index. For example, the vector v1 [] = {1, 2, 3}, v2 [] = {4, 5, 6}. The answer should be v3 [] = {5,7,9}. - fedor-sg
  • Well then, describe the assignment operator, in which you create and return a new object of class V (do not forget to just check the dimension equality for the terms), and then perform the assignment. Only you still have the copy constructor (like the second one) not working - you copied the dimension, and the data? ... That's about the way you need to assign it - copy the dimension and transfer the data ... - Harry
  • Usually the copy constructor looks like this: const V & V :: operator = (const V & obj) {return * this; }. But how to copy the data? - fedor-sg
  • And this is what the constructor looks like for addition (+): VV :: operator + (V obj). But I don’t understand what to write in the body. - fedor-sg

1 answer 1

Well, even though it is very non-pedagogical ...

 class V { public: V(int dimension = 1):dimension(dimension) { // Выделять память кто будет?? ptrdata = new double[dimension]; } V(const V &obj) : dimension(obj.dimension) { // Выделять память кто будет?? ptrdata = new double[dimension]; // Копируем данные for(int i = 0; i < dimension; ++i) ptrdata[i] = obj.ptrdata[i]; } ~V(){ delete [] ptrdata; } V& operator=(const V& obj) { // Присваивание самому себе if (this == &obj) return *this; // Если размер другой - надо создать новые данные if (dimension != obj.dimension) { delete[] ptrdata; dimension = obj.dimension; ptrdata = new double[dimension]; } // Копирование for(int i = 0; i < dimension; ++i) ptrdata[i] = obj.ptrdata[i]; return *this; } V operator+(const V& obj) const { // Если размеры не совпадают - какое уж тут суммирование... assert(dimension == obj.dimension); V res(dimension); // Сами разберетесь, как суммируем?! for(int i = 0; i < dimension; ++i) res.ptrdata[i] = ptrdata[i] + obj.ptrdata[i]; return res; } void Fill(); void Show() const; private: int dimension; double* ptrdata = 0; }; 

Of course, frankly, you can do better. Add a constructor and an assignment move, for assigning use the copy-exchange idiom ... But all this later, until you figure out what is.

Yes, and also - pull out the srand in main - accuracy in the same place - second, it is unlikely that you have a super-slow EC-1840, and it would fill up faster :) - so in your Fill variant the arrays will be the same.

Yes, after: don’t allocate memory in Fill ! For this there is a designer! And you can neither fold the initial Fill call, for example, or withdraw, nor make a couple of Fill calls without a memory leak!

  • Everything is working! Thanks for the help! I will learn to grow in the C ++ philosophy :) :) - fedor-sg