c++ implicit conversion of string to char* matches wrong function signature -


I am writing a program that uses c string (char *) and c ++ string (std :: string). I have separated from worry for the example given below.

  #include & lt; Iostream & gt; #include & lt; String & gt; Hello zero (std :: string s) {std :: cout & lt; & Lt; "STRING function" & lt; & Lt; Std :: endl; } Zero hello (four * c) {std :: cout & lt; & Lt; "Chard Function" & lt; & Lt; Std :: endl; } Int main (int argc, char * argv []) {hello ("obscure"); Hi (std :: string) "string"); Hi ((four *) "charp"); Return 0; }   

When I compile this program, I get a warning:

  test.cpp: 14: Warning: string continuous one Conversion to dislike ?? ?? Four * one ???? Regarding the first call on   

Hello . Program running:

  CHAR function STRING function charge function   

It is showing that Hello with the first call signature Matching Hello (four * c) .

My question is, if, as a C ++ program, a string literally, ( "obscure" ) is a Std :: string, it's a Why insert for char * and then Hello (four * c) to std :: string and match hello (std :: string s) ?

I know I can pragma or - Warning something out (and I can put the string * four without worrying), but I want to know why the compiler will bother this artist And if there is no way to tell it. I am compiling with G ++ 4.4.3.

Thank you.

string literals such as "obscure" type std :: string are not std :: string A library is the only type, which is not a language magic. The type of a string is actually const char [n] , where N is literal length

for historical reasons (backward compatibility) string literals Converting them to char * will convert them (violation of constipation). This built-in conversion "user defined" conversion is sent to std :: string , which is why it calls the char * function and warns you < / P>

If you change the signature of Hello Hello (const char * c) , then you probably will not have to give a warning (but still To call ' std :: string edition, which requires a manual artist)

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -