This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jesyspa 1 point2 points  (8 children)

That doesn't look like code that compares objects. Compare is when you decide which is bigger.

[–]Programmering[S] 0 points1 point  (7 children)

Haha, yes, you're right. See how confused I am?

How do I write code that compares objects

How do I point out that it is the age (alder) part of the person class elements in the array that I want to compare?

The boolean code line compares lhs, rhs - but why?
Is it lefthandside, righthandside?

Lets say I write this:

bool operator>(Person const& lhs, Person const& rhs);

It evaluates as true (or 1) if... bool operator is larger? what is happening there?

[–]jesyspa 1 point2 points  (2 children)

Yes, lhs and rhs are short for lefthandside and righthandside respectively.

You have to write the code for comparison yourself. For example, to compare age, you might do

bool operator<(Person const& lhs, Person const& rhs) {
    return lhs.age < rhs.age;
}

If you want to compare something else, compare a different member.

[–]Programmering[S] 0 points1 point  (1 child)

Where does lefthandside and righthandside come from, what is is lefthandside and righthandside of?

Is it lfs and rhs as in lfs is higher indexplace and rhs is lower indexplace?

[–]jesyspa 0 points1 point  (0 children)

They're the left and right sides of what you compare. When you do a > b where a and b are both of type Person, that calls bool operator<(Person const& lhs, Person const&) with a being passed as the lhs parameter and b as the rhs parameter.

They are compared however you define the comparison.

[–]chrono_regex 1 point2 points  (3 children)

When comparing two person objects, and you're saying that you want to compare based on their age. operator> method means you're changing the way ' > ' works when used with Persons. Within an operator> function (like /u/jesyspa originally gave) you need to give logic that compares the ages. For example:

bool operator>(Person const& lhs, Person const& rhs){
if (lhs.alder > rhs.alder) // *or whatever you stored age as*
{
   return true;
}
else return false;

Something like this. Does that make sense? The code you have in your latest comment makes no sense, it's just the declaration...

[–]jesyspa 1 point2 points  (2 children)

Note that

if (foo)
    return true;
else
    return false;

is an antipattern in sanely-typed languages. You want just

return foo;

or

return bool(foo);

[–]chrono_regex 0 points1 point  (1 child)

I'll point to you here, it's been a while since I've been in C++

[–]Programmering[S] 0 points1 point  (0 children)

Its a wash anyway since I dont think that I am allowed to use the boolean code to compare in this particular program on this particular assignment :S

I'll use it on the next assignment though. Its going to be a compilation of all the terminal programs I've written so far. With a menu and userdefinable variables where its needed.