Thread: STL - Overriding operator < and == for "user defined types"

  1. #1
    Registered User
    Join Date
    Jan 2011

    STL - Overriding operator < and == for "user defined types"

    As per The Complete reference C++ books, if we are planning to use an "user defined type" in an STL class like vector<MyType>, we need to atleast override the operator < () and operator == ().

    1. Is it true? If yes, reason to override operator < () and operator ==().

    2. Do we need to define these as member functions or global functions:

    bool operator < (MyType first, MyType second) //global func()

    bool operator < (MyType &rhs) //member func()

  2. #2
    Registered User
    Join Date
    Jun 2005
    No, it is not strictly necessary to define comparison operators (== or <) for a user defined type that you will place in a container. It is necessary if you want to do certain things to that container, such as sorting it or searching for particular values. Or if you want to retrieve elements from the container and compare their value with something else.

    It is necessary to provide functional assignment operators, default and copy constructors, and destructors (and possible one or two other things that escape my memory right now). Beyond that, different containers may require support of some additional operations - for some particular containers (eg maps) which find values using keys, it is necessary to be able to compare keys.

    If you supply the comparison operations, either of the two forms (member or global function) are acceptable. The only requirement is avoiding ambiguity .... the compiler will get upset if you provide both forms, as it has no basis for deciding which one to use. Generally comparison operators should not modify their operands, so their specification should make liberal use of the const keyword.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2011


    Thanks I'll take your word on it !!!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    By the way, you probably mean "overload" rather than "override" here because "override" is typically used with respect to the override of a virtual function in a base class by a virtual function with the same name, signature and covariant return type in a derived class.
    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. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  2. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM