Where to define operator<?

C

cppaddict

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(vectOfA.begin(),vectOfA.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
 
J

John Harrison

cppaddict said:
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(vectOfA.begin(),vectOfA.end());
</code>

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

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


No, it will only compile if it has access to the declaration, the definition
is not necessary.
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?

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

john
 
D

Daniel T.

cppaddict said:
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(vectOfA.begin(),vectOfA.end());
</code>

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

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

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

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

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?

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() );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top