I have a program with a class Length. This class has an attribute named size of type int and a dynamic
array *numb of type char. I have overloaded operators << and = so that I can print object values and assign values of an object to another.
If I leave the return type of operator = to void, the program seems to work fine but if I try to return a Length object I get junk printed. Why?. Thank you.
Here is my code:
class Length
{
    int size;
    char *numb;
public:
    Length()
    {
    }
    Length(int size, char *n);
    ~Length();
    friend ostream & operator << (ostream &channel, const Length &l);
    Length operator = (const Length &x);
};
Length::Length(int size, char  *n)
{
    this->size = size;
    numb = new char[size];
    for(int i = 0; i < size; i++)
    {
        numb[i] = n[i];
    }
}
ostream & operator << (ostream &channel, const Length &l)
{
    channel << "Size " << l.size <<endl;
    for(int i = 0; i < l.size; i++)
    {
        channel << l.numb[i] << endl;
    }
    return channel; 
}
Length Length::operator =(const Length &x)
{
    
    delete [] this->numb; 
    this->size = x.size;
    this->numb = new char[this->size];
    memcpy(this->numb, x.numb, this->size);
    return *this; //If I comment that line and make return type void programm works fine
}
int main()
{
    char * ch = "Hello";
    char * cx = "Hi";
    int size = strlen(ch);
    int size_x = strlen(cx);
    Length h(size, ch);
    Length x(size_x, cx);
    cout << x; //Works fine
    cout << h <<endl; //Works fine
    x = h; 
    cout << x <<endl; //Prints junk
}
 
     
     
    