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

This is a discussion on STL - Overriding operator < and == for "user defined types" within the C++ Programming forums, part of the General Programming Boards category; As per The Complete reference C++ books, if we are planning to use an "user defined type" in an STL ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    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
    Posts
    6,255
    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%.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Thanks

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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21