Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • u dont know if gettitle() returns a temporal string object, if that's the case, this code will yield undefined behaviour when trying to use c_str Commented Oct 30, 2011 at 2:56
  • thanks that took care of that part. didnt know i needed to put .c_str() at the end Commented Oct 30, 2011 at 2:56
  • @smerlin: Yes, that is a possibility. I'll fix my answer to address that. Commented Oct 30, 2011 at 2:57
  • The reason is this: If a temporary is returned. It is destroyed after the statement ends. At which the pointer returned by c_str() is no longer valid. I have updated my answer to address this issue. Commented Oct 30, 2011 at 3:00
  • you can either store the std::string returned by gettitle() in a named std::string variable, or pass the const char* returned by c_str() directly to strcmp. If gettitle() returns a std::string& or a const std::string&, the old code was fine. Commented Oct 30, 2011 at 3:00