How to allow a std:string parameter to be NULL?

前端 未结 3 1356
臣服心动
臣服心动 2021-01-17 22:21

I have a function foo(const std::string& str); that it does crash if you call it using foo(NULL).

What can I do to prevent it from cras

3条回答
  •  盖世英雄少女心
    2021-01-17 22:44

    You have a function that accepts a std::string, so provide it an std::string, not a pointer.

    foo(std::string());
    

    This will provide the function with an empty string, which is probably what you would have interpreted your null value as anyhow.

提交回复
热议问题