Thread: stl string overloaded operators

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    stl string overloaded operators

    Hey,

    I don't typically use C++, I'm more of a C guy, but when using stl strings (std::string), as far as the == operator goes.

    I go here for all my references for C++ and C.
    cplusplus.com - The C++ Resources Network

    I'm sure many others do, too. But it doesn't list == as an overloaded operator, with the semantics of .compare(). Though == works, apparently.

    Did cplusplus.com just forget it or something?? Is it not officially part of the STL spec? What gives?

    EDIT:
    Forgot to say, thanks in advance.

    Also, I know there's others like gotapi.com and cppreference.com - I just typically used cplusplus.com and was wondering if there was a particular (obvious) reason why it wasn't there..
    Last edited by Syndacate; 12-11-2010 at 02:03 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    operator == is not a member of the std::string class, if that's what you mean. It's just a free function.

    (EDIT: And it doesn't have the semantics of .compare() either -- operator== always returns true/false, while .compare() returns negative/0/positive like strcmp.)

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    As tabstop mentioned, it (they, actually) are global operators, not members. The reference is here:
    comparison operators - C++ Reference
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    I get that they're not member functions, and yeah, I see the difference between that and compare, but are you trying to say that they're not overloaded for the string class? If so, how do they end up working for std::strings?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Syndacate
    I get that they're not member functions, and yeah, I see the difference between that and compare, but are you trying to say that they're not overloaded for the string class? If so, how do they end up working for std::strings?
    They are overloaded for the std::string class as non-member functions. One possible reason is so that there can be an overload of operator== for std::string which takes a const char* on the left hand side. This would not be possible with a member function.

    That said, there are many other member functions of std::string that could have been non-member non-friend functions. Read GotW #84: Monoliths Unstrung.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM