Ok, when I call function print() on A, this is the output:
getString(): �����
getString2(): Example
I know that getString() method can't work, because memory reserved for local variable tmp will be cleaned at the end of the function; but why getString2() works right? Is not tmpStr a local variable like tmp?
When you return a string, C++ uses its copy constructor to copy the returned object (tmpStr) and a copy is given to the caller (A::print()). The copy is discarded for sure when the scope is gone.
Yes, but strings are objects, and thus the copy constructor is called. That will copy the data correctly. char arrays are not, and thus you only "copy" the address (which like you said, is garbage).