Why does std::string not provide a conversion to const char*?

前端 未结 4 1391
小鲜肉
小鲜肉 2020-12-03 11:30

This is more of a policy or a historical question. Why was it decided not to provide a const char * conversion for std::string? Were there a fear someone might do p

4条回答
  •  青春惊慌失措
    2020-12-03 12:10

    Automatic casts are almost always evil. If there were a cast to const char *, a std::string could be also automatically cast to other pointer types and that could lead to hard to find bugs. There is the c_str() method that returns const char * so you can still achieve what you need. Also, the typecast is not logically correct - std::string is not equivalent to const char *.

提交回复
热议问题