c++ - returning NULL in cost string& function -


I have to write my own famous map on C ++. This is a method. Get ("some_string") If this string is not in my hash_map, then I return NULL. But I can not check in my program if this function returns a null or string. This is my code:

  if (m.get ("some_string")) cout & lt; & Lt; M.get ("some_string");  

More method:

  Constring & amp; Map :: Mill (const string & amp;) {string_node * pos = find_pos (s); If (pause) returns pos- & gt; Val; And return of return; }  

pos-> val is just a string so I have an error that I can not convert from a string to a string in the question. The question is what do I need to do, Cout to prevent errors, then returns my work NULL . How can I check it?

You can not return a blank value for the const string . You have to return a reference to a real string object that will survive after returning the function.

option:

  • return a const string *

  • Return a string from the value,

  • And return empty (but valid) string to an empty case. It has two disadvantages, it copies the results and you can not see the difference between a null case and the actual empty string value, without having to output more information than without the function.


Comments