Steve555
12/7/2008 1:08:00 PM
On 7 Dec, 12:45, zr <zvir...@gmail.com> wrote:
> On Dec 7, 1:53 pm, Steve555 <foursh...@btinternet.com> wrote:
>
>
>
> > Hi,
>
> > I've ended up with many, many operator< functors like this
>
> > bool operator<(UserRating lhs, UserRating rhs) {
> > return lhs.userID < rhs.userID;
>
> > }
>
> > to sort the many custom structs I have, and the typedefined vectors I
> > have to store them.
> > All of the structs and their vectors are neatly organized in to one
> > myVecDefiness.h file, but the functors are dotted about all over the
> > place. If I try to put them in myVecDefiness.h (or any other header),
> > then the compiler complains of multiple definitions (even though I
> > have #ifndef etc. in the header). But if I put them in a single .cpp
> > then the compiler doesn't find them.
>
> > What's the neatest way to organize this?
>
> > Thanks
>
> > Steve
>
> Just a thought.
> If all of your classes contain an ID field, you could consider
> inheriting them all from a common base class that contains that field.
> That way you will only need to define one comparison operator for the
> base class.
>
> class BaseClass
> {
> friend bool operator<(const BaseClass& a, const BaseClass& b);
> //...
> protected:
> int id;
>
> };
>
> bool operator<(const BaseClass& a, const BaseClass& b)
> {
> return a.id < b.id;
>
> }
>
> class A : public BaseClass
> {
> //...
>
> };
>
> class B : public BaseClass
> {
> //...
>
> };
>
> Beware: this will allow comparisons between objects of different types
> if their classes inherit from BaseClass:
> A a;
> B b;
> a < b; //ok
Thanks for the idea, that would clean things up a lot, but I'm not
sure if I can afford the over head of converting mu structs to classes
as I'm trying to fit millions in to limited memory. Any idea what the
cost of this would be on a typical struct that might contain two
shorts, one long, and a float?