Operator override when using STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • meng.frank@gmail.com

    #1

    Operator override when using STL

    If I want to use std::vector or std::list for my class MyNewClass,
    which operators should I override?
    Maybe operator new and operator delete?
    If I want to use MyNewClass in std::set or std::map, which operators
    should I override in order to make the key unique?
    Thank you for your help,

  • Axter

    #2
    Re: Operator override when using STL

    meng.frank@gmai l.com wrote:[color=blue]
    > If I want to use std::vector or std::list for my class MyNewClass,
    > which operators should I override?
    > Maybe operator new and operator delete?
    > If I want to use MyNewClass in std::set or std::map, which operators
    > should I override in order to make the key unique?
    > Thank you for your help,[/color]

    For std::vector or std::list you don't need to override any operators.
    It may be a good ideat to create an assignement operator for your
    class.
    foo& operator=(const foo&);

    If your class has dynamic memory storage via raw pointer, you usually
    do want to implement an assignment operator.

    for std::set or std::map you want to add a comparison operator to your
    class.
    bool operator<(const foo& lhs, const foo& rhs);

    ----------------------------------------------------------------------------------------
    David Maisonave


    Author of Axter's policy based smart pointers
    (http://axter.com/smartptr)
    Top ten member of C++ Expert Exchange:
    Get answers to your C++ questions from our top industry experts by using Experts Exchange's platform to find the best C++ help & solutions. Click to learn more!

    ----------------------------------------------------------------------------------------

    Comment

    • Marcus Kwok

      #3
      Re: Operator override when using STL

      Axter <google@axter.c om> wrote:[color=blue]
      > meng.frank@gmai l.com wrote:[color=green]
      >> If I want to use std::vector or std::list for my class MyNewClass,
      >> which operators should I override?
      >> Maybe operator new and operator delete?
      >> If I want to use MyNewClass in std::set or std::map, which operators
      >> should I override in order to make the key unique?
      >> Thank you for your help,[/color]
      >
      > For std::vector or std::list you don't need to override any operators.
      > It may be a good ideat to create an assignement operator for your
      > class.
      > foo& operator=(const foo&);
      >
      > If your class has dynamic memory storage via raw pointer, you usually
      > do want to implement an assignment operator.[/color]

      Also, keep in mind the "rule of 3": if you need to implement any of
      {assignment operator, copy constructor, destructor} to manage resources,
      chances are that you probably should implement all 3, or at least make
      them private so that they cannot be called.
      [color=blue]
      > for std::set or std::map you want to add a comparison operator to your
      > class.
      > bool operator<(const foo& lhs, const foo& rhs);[/color]

      --
      Marcus Kwok

      Comment

      • meng.frank@gmail.com

        #4
        Re: Operator override when using STL

        Thank you for your responses.
        How to make std::set or std::map 's key unique?
        If user tries to insert an object with an existing key, it should fail.
        Right?
        If I override comparison operator, how does that guarantee the key is
        unique?
        Should I search the new key from the std::set or std::map list?
        How to do it in my class MyNewClass? Or somewhere else?

        Comment

        • Marcus Kwok

          #5
          Re: Operator override when using STL

          meng.frank@gmai l.com wrote:[color=blue]
          > Thank you for your responses.
          > How to make std::set or std::map 's key unique?
          > If user tries to insert an object with an existing key, it should fail.
          > Right?
          > If I override comparison operator, how does that guarantee the key is
          > unique?
          > Should I search the new key from the std::set or std::map list?
          > How to do it in my class MyNewClass? Or somewhere else?[/color]

          Hi,
          Please read http://cfaj.freeshell.org/google/ so that you can properly
          quote what you are replying to.

          std::set and std::map by default use the 'less-than' operator
          ("operator<" ) for comparison. IIRC, it determines equality using <; for
          example:

          if ((a < b) || (b < a)) then they are not equal

          which is equivalent to:

          if (!(a < b) && !(b < a)) then they are equal

          Using this criteria, it determines equality between your key and the
          already existing keys. If your key is not equal to any other keys, then
          it adds it to the set/map.

          You can use the .insert() functions to see whether the insertion
          succeeded or failed. Be sure to look up the documentation since the
          interface is a little unusual (hint: it returns a
          std::pair<itera tor, bool>).

          --
          Marcus Kwok

          Comment

          • Markus Schoder

            #6
            Re: Operator override when using STL

            meng.frank@gmai l.com wrote:[color=blue]
            > Thank you for your responses.
            > How to make std::set or std::map 's key unique?
            > If user tries to insert an object with an existing key, it should fail.
            > Right?
            > If I override comparison operator, how does that guarantee the key is
            > unique?[/color]

            Its a property of set and map that they do not allow dupliate keys. The
            insert function of these containers will not insert an element whose
            key already is there. Remember the insert function returns a pair
            consisting of the iterator of the newly inserted resp. already existing
            element and a bool value indicating wether the element was inserted or
            not.

            Comment

            Working...