The following is a train of thought; I do not offer this as an authoritative answer, as I'd have to dig out the standard and check the rules and so forth regarding temporary variables.
When you do this, myclassA.cadena() returns a temporary variable that has no name. If you were passing by value, a copy of it would be made and passed into writeS, so no problem. However, you're trying to pass by reference a temporary variable with no name. A reference is essentially an alias for something; it's just another name for a variable.
I'm not sure of the exact internal workings, but I'm not surprised it doesn't work.
It should work with a temporary object because the temp object should be destroyed only after the function call that uses it is complete. I was also unaware that you could return a C string and compilers would change that to a std::string automatically. If you change cadena() to return std::string("hello");, does it make any difference?
It should work with a temporary object because the temp object should be destroyed only after the function call that uses it is complete.
Not saying you're wrong; more thinking out loud. How do you reference an object with no name? I really don't know enough about the reference mechanism to know if it can be done or not. I've always thought of a reference essentially as an alias, another name for something that already has a name.
Thinking out loud again, at a tangent,
"hello"
is a different kettle again, as it is a string literal and exists for the lifetime of the program, from start to finish.
"hello"
exists essentially forever, but the std::string created from it is temporary.
I got in trouble the first time I tried it, but I solved the problem by making UseString()'s only parameter const. Maybe all that is needed is to make it const.
I'll clone it change MyString() the way I suggested above and test.