How does std :: string control this trick?

I just wrote a function:

void doSomeStuffWithTheString(const std::string& value) {
...
std::string v = value;
std::cout << value.c_str();
...
}

but then I call it with

doSomeStuffWithTheString("foo");

and it works. Therefore, I would think that to work (const char * to initialize an implicit instance of std :: string), the value must be passed by value, but in this case it is passed by reference (const).

Is randomly implicit temporary std :: string created from const char * when the link is const? if not, how is it possible to work?

EDIT

what happens if a function is overloaded with

void doSomeStuffWithTheString(const char* value);

which one will the compiler choose?

+5
source share
4 answers

std::string ( ) const char*. "foo" std::string. . ++ const & , , .

, ++.

class Example {
public:
  Example(const char* pValue) {}
};

void Method(const Example& e) {
  ...
}

Method("foo");
+7

, std::string .

0

, std::string

0

" , (const)."

++, ( std::string const char *) const-reference ( const std::string &).

0

All Articles