C++ overloading conversion operator for custom type to std::string

前端 未结 5 733
無奈伤痛
無奈伤痛 2020-12-30 03:52

I hope someone might be able to answer why the following doesn\'t work. Bear with me though, I am still very much a noob... I just cannot get to the bottom of why the follow

5条回答
  •  爱一瞬间的悲伤
    2020-12-30 04:48

    There's no exact std::string::operator=. The candidates are, paraphrased,

    s = (const std::string)(std::string)t;
    s = (const char*)t;
    s = (char)(int)t;
    

    I think things will work if you change it to return const std::string. (EDIT: I'm wrong.) Also note that the first function should return const char *. If you need to cast a string literal to char*, you're doing something wrong; string literals are not writeable.

提交回复
热议问题