3
I’m learning about overload operators, but I don’t understand why the return at the end of the method. It would not be enough just to create new memory allocation?
StringBad & StringBad::operator=(const StringBad & st)
{
cout<<"**********USANDO O OPERADOR************";
if (this == &st) // object assigned to itself
return *this; // all done
delete [] str; // free old string
len = st.len;
str = new char [len + 1]; // get space for new string
std::strcpy(str, st.str); // copy the string
return *this; // return reference to invoking object
}
This is the main()
:
StringBad headline1("Celery Stalks at Midnight");
StringBad knot;
knot =(headline1);
cout << "knot: " << knot << endl;
cout << "Exiting the block.\n";
cout << "End of main()\n";
then the return is a rule in this case is mandatory
– dougllas
Yes, according to the language specification.
– Maniero
OK THANKS AGAIN !!! YOU ARE A BEAST THANK YOU EVEN
– dougllas
@dougllas you can already vote on everything on the site, see the [tour].
– Maniero