简体   繁体   中英

C++ typecasting when passing std::string as function argument

Is passing a string by "" equivalent to passing a string by calling std::str("") in C++?

eg given a function which accepts std::str as an argument:

void funcA(std::string arg) {
    arg = "abc";
}

Should I call it by funcA(std::string("abc")); or funcA("abc"); ? ie is the second version a typecast from an array of char?

They are equivalent. Because the constructor std::string::string( char const * ) is not declared as explicit , it is called implicitly to provide a conversion from char * to string . The implicit call does the same thing as the explicit call (written out as std::string("abc") ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM