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
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