I want to overload + operator so that you can add two objects of type myfunnyarray(data type which behave like a built-in C++ array of integers). The resulting object should be the concatenation of the arrays of the two operands of addition. However for some reason the result is consisting of garbage values.
here is my code :
#include <iostream>
using namespace std;
class myfunnyarray{
int *x;
public:
myfunnyarray(int size){
x = new int[size];
for(int i=0;i<size;i++)
x[i]=0;
}
myfunnyarray(int size, int value){
x = new int[size];
for(int i=0;i<size;i++)
x[i]=value;
}
~myfunnyarray(){
delete [] x;
}
myfunnyarray operator+(myfunnyarray obj)
{
int size = sizeof(x);
int temp = sizeof(obj.x);
myfunnyarray result(size + temp);
cout<<result<<endl;
for (int i = 0; i < size; i++)
{
result.x[i] = x[i];
cout<<result<<endl;
}
for (int i = size; i < size + temp; i++)
{
result.x[i] = obj.x[i - size];
}
return result;
}
friend ostream &operator<< (ostream &out, myfunnyarray o);
};
ostream &operator<< (ostream &out, myfunnyarray o){
for (int i = 0; i < sizeof(o.x)+1; i++)
{
out << o.x[i];
if (i < sizeof(o.x) )
{
out << ",";
}
}
return out;
}
int main(){
myfunnyarray s(5);
cout<<saim<<endl;
myfunnyarray p(5,10);
cout<<paim<<endl;
myfunnyarray q = s +p;
cout<<q;
}