Although in C++ it is possible, it is generally not advised to inherit from standard classes, see Why should one not derive from c++ std string class? for more info.
I am wondering, what is the function of the assignment of
str = t;
str is a std::string type, t is Test type. So what is the expected value of the assigment? No one can guess. I suggest to explicitly call a conversion method or an operator for code clarity.
This would make your example look like:
str = t.convertToString();
or the more standard way is to implement the stream operator, which makes
str << t;
(Note this example works if str is a stream type, if it is a string, you need further code, see C++ equivalent of java.toString?.)
But, if you really want it to work without a conversion method, you can override the string assignment operator of Test:
class Test {
public:
operator std::string() const { return "Hi"; }
}
See also toString override in C++
Although this is a perfect solution to your question, it may come with unforeseen problems on the long run, as detailed in the linked article.