Where to define operator<?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cppaddict

    Where to define operator<?

    Hi,

    The question of where to define operator< came up when I was using the
    std::sort method. Let's say I have a container whose members (of
    class A) I need to sort. In particular, we have some client code that
    looks like this:

    <code>
    #include A.h

    std::vector<A> vectOfA;

    //add some elements to vectOfA here

    std::sort(vectO fA.begin(),vect OfA.end());
    </code>

    The code above will only compile if it has access to the definition

    bool operator<(const A& x, const A& y)

    And the only way that could happen (in the example above) is if that
    definition is in A.h. Which would imply that header files are the
    standard place to put the operator< definition, which doesn't sound
    right to me. Where should I put it so that client code can always
    access it?

    Thanks,
    cpp

  • John Harrison

    #2
    Re: Where to define operator&lt;?


    "cppaddict" <hello@hello.co m> wrote in message
    news:oj1lc0l9u4 ck425k5jbgko8lb ji3v2eesn@4ax.c om...[color=blue]
    > Hi,
    >
    > The question of where to define operator< came up when I was using the
    > std::sort method. Let's say I have a container whose members (of
    > class A) I need to sort. In particular, we have some client code that
    > looks like this:
    >
    > <code>
    > #include A.h
    >
    > std::vector<A> vectOfA;
    >
    > //add some elements to vectOfA here
    >
    > std::sort(vectO fA.begin(),vect OfA.end());
    > </code>
    >
    > The code above will only compile if it has access to the definition
    >
    > bool operator<(const A& x, const A& y)[/color]


    No, it will only compile if it has access to the declaration, the definition
    is not necessary.
    [color=blue]
    >
    > And the only way that could happen (in the example above) is if that
    > definition is in A.h. Which would imply that header files are the
    > standard place to put the operator< definition, which doesn't sound
    > right to me. Where should I put it so that client code can always
    > access it?
    >[/color]

    Put the declaration in A.h, put the definition in A.cpp, same as any other
    function or operator.

    john


    Comment

    • Daniel T.

      #3
      Re: Where to define operator&lt;?

      In article <oj1lc0l9u4ck42 5k5jbgko8lbji3v 2eesn@4ax.com>,
      cppaddict <hello@hello.co m> wrote:
      [color=blue]
      >Hi,
      >
      >The question of where to define operator< came up when I was using the
      >std::sort method. Let's say I have a container whose members (of
      >class A) I need to sort. In particular, we have some client code that
      >looks like this:
      >
      ><code>
      >#include A.h
      >
      >std::vector< A> vectOfA;
      >
      >//add some elements to vectOfA here
      >
      >std::sort(vect OfA.begin(),vec tOfA.end());
      ></code>
      >
      >The code above will only compile if it has access to the definition
      >
      >bool operator<(const A& x, const A& y)[/color]

      Well, it only needs the declaration not the definition. IE:

      bool operator<(const A& lhs, const A& rhs); // note the ';'

      [color=blue]
      >And the only way that could happen (in the example above) is if that
      >definition is in A.h. Which would imply that header files are the
      >standard place to put the operator< definition, which doesn't sound
      >right to me. Where should I put it so that client code can always
      >access it?[/color]

      That depends. If the op< that you are using for this sort routine is
      generally useful for all clients that use 'A' then put it in A.h. If it
      is spicific to this program/module, put it in the program/module's .h
      file. For example:

      class Person {
      public:
      string lastName() const;
      unsigned zipCode() const;
      //...
      };

      We could sort the class above by lastName or zip and there is no reason
      to prefer one over the other so op< should not be defined in Person.h...
      We write one program that uses the Person class to find people who live
      near each other...

      // NeighborFinder. h
      bool operator<(const Person& lhs, const Person& rhs);

      // NeighborFinder. cpp
      bool operator<(const Person& lhs, const Person& rhs) {
      return lhs.zipCode() < rhs.zipCode();
      }

      //...

      sort( people.begin(), people.end() );

      Comment

      Working...