jhtong
jhtong

Reputation: 1589

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++?

e.g. 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"); ? i.e. is the second version a typecast from an array of char?

Upvotes: 2

Views: 688

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137820

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")).

Upvotes: 4

Related Questions