[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Best way to organize my sorting funtcors?

Steve555

12/7/2008 11:53:00 AM

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

Kai-Uwe Bux

12/7/2008 12:04:00 PM

0

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

The comparison above is a free-standing function, but nonetheless part of
the public interface of UserRating. I would put its declaration with the
declaration of that struct and its implementation with the implementation
of all other parts of the interface.

BTW: I would only overload operator< if it makes sense. Usually, a
well-named comparison function will be more clear (but has to be passed to
the algorithms explicitly).


Best

Kai-Uwe Bux

Steve555

12/7/2008 12:26:00 PM

0

On 7 Dec, 12:03, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Steve555 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?
>
> The comparison above is a free-standing function, but nonetheless part of
> the public interface of UserRating. I would put its declaration with the
> declaration of that struct and its implementation with the implementation
> of all other parts of the interface.
>
> BTW: I would only overload operator< if it makes sense. Usually, a
> well-named comparison function will be more clear (but has to be passed to
> the algorithms explicitly).
>
> Best
>
> Kai-Uwe Bux

Thanks Kai, does that mean I should then find anywhere I've used
std::binary_search() etc. and manually add the "well-named comparison
function" as the last parameter?

zr

12/7/2008 12:45:00 PM

0

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

Steve555

12/7/2008 1:08:00 PM

0

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?

Pete Becker

12/7/2008 1:19:00 PM

0

On 2008-12-07 08:07:43 -0500, Steve555 <foursheds@btinternet.com> said:

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

Try it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Steve555

12/7/2008 1:27:00 PM

0

On 7 Dec, 13:18, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-12-07 08:07:43 -0500, Steve555 <foursh...@btinternet.com> said:
>
>
>
> > 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?
>
> Try it.
>
> --
>   Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)

It's not laziness honest! ;-)... but I have 20+ classes to write, as
well as the code that references them I'll rephrase the question: Is
there a way to calculate the size of one such class for comparison? I
thought the class itself with the minimum constructor etc. took up
some amount of bytes?

zr

12/7/2008 1:33:00 PM

0

On Dec 7, 3:07 pm, Steve555 <foursh...@btinternet.com> wrote:
> 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?

See these topics:
http://www.parashift.com/c++-faq-lite/classes-and-objects.ht...
http://www.parashift.com/c++-faq-lite/virtual-functions.htm...
Bottom line: if you do not define any virtual method in your class,
the overhead is zero.

Steve555

12/7/2008 1:49:00 PM

0

On 7 Dec, 13:32, zr <zvir...@gmail.com> wrote:
> On Dec 7, 3:07 pm, Steve555 <foursh...@btinternet.com> wrote:
>
>
>
> > 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?
>
> See these topics:http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8http://www.parashift.com/c++-faq-lite/virtual-functions.htm...
> Bottom line: if you do not define any virtual method in your class,
> the overhead is zero.

Yup, to quote from the first link:
"A class feels like a living and responsible member of society with
intelligent services, a strong encapsulation barrier, and a well
defined interface."
Exactly my mental block. I'm glad we got on this topic because I think
I'll be less hesitant about using classes in future.

joseph cook

12/7/2008 2:24:00 PM

0

On Dec 7, 8:26 am, Steve555 <foursh...@btinternet.com> wrote:
> On 7 Dec, 13:18, Pete Becker <p...@versatilecoding.com> wrote:
> Is
> there a way to calculate the size of one such  class for comparison? I
> thought the class itself with the minimum constructor etc. took up
> some amount of bytes?

Nope, you are mistaken. For your purposes, 'class' and 'struct' are
totally synonymous, except one defaults to 'public' and one defaults
to 'private'. That's it.

Joe C

Steve555

12/7/2008 2:42:00 PM

0

On 7 Dec, 14:23, joec...@gmail.com wrote:
> On Dec 7, 8:26 am, Steve555 <foursh...@btinternet.com> wrote:
>
> > On 7 Dec, 13:18, Pete Becker <p...@versatilecoding.com> wrote:
> > Is
> > there a way to calculate the size of one such  class for comparison? I
> > thought the class itself with the minimum constructor etc. took up
> > some amount of bytes?
>
> Nope, you are mistaken.   For your purposes, 'class' and 'struct' are
> totally synonymous, except one defaults to 'public' and one defaults
> to 'private'.  That's it.
>
> Joe C

Thanks Joe, it's an eye-opener.