I have a class that contains an array data. I need to write an overloaded operator that returns an object of the class with the addition of two class arrays. For example: Container x({1,2,3}) + y({1,2,3}).
I tried the following and numerically it works, but it doesn't return a new object. I also included the constructors.
Constructors:
Container(){
length = 0;
data = nullptr;
}
Container(int len){
length = len;
data = new double[length];
}
Container(std::initializer_list<double> il): Container(il.size())
{
std::copy(il.begin(), il.end(), data);
}
Attempt to overload:
Container& operator+(const Container& other)
{
for (auto i=0; i<other.length; i++){
data[i] = data[i] + other.data[i];
}
return *this;
Could I please get feedback on this?