Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:26:16+00:00 2026-05-31T13:26:16+00:00

Suppose we have an object which represent a box. class Box { public: int

  • 0

Suppose we have an object which represent a box.

class Box {
  public:
    int length;
    int width;
    int height;

    Box(int l, int w, int h);
    ~Box();
    int area(const int l, const int w) const;
    int volume(const int l, const int w, const int h) const; 
};

Now lets say in another class, in another file, we have an std::list<Box> that contains n boxes. We want to display a print out of these boxes in different ways.

  • First we want to print them out in order of increasing size by their lengths.
  • Then we want to print them out in order of increasing size by their widths.
  • We don’t care about their heights right now, but maybe later we will.

Now std::list has a member function sort() that takes a comparison function as an argument. How can we alter the above Box class so that we can call sort() on our list with different comparison functions?

More specifically, can we define three different functions inside the box class and simply pass them to list.sort()? Is it better to define the comparison functions globally outside of the class Intuitively, tying them to the class seems better but why would this not be the case? Overall, what is the “best” way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:26:16+00:00Added an answer on May 31, 2026 at 1:26 pm

    If you really want to implement the three different comparison
    operations as members you can do something like this:

    struct Box {
      int x, y, length;
    
      bool less_by_x(const Box& other) { return this.x < other.x; }
      bool less_by_y(const Box& other) { return this.y < other.y; }
      bool less_by_length(const Box& other) { return this.y < other.y; }
    };
    

    and use them like this:

    #include <functional>
    // you probably shouldn't be using a list anyway
    std::list<Box> l; 
    l.sort(std::mem_fun_ref(&Box::less_by_x));
    

    mem_fun_ref returns an binary function object that takes a reference to the class of which the function is a member and all the rest of the arguments (only in C++11, this is limited to binary functions in C++03) of the member functions.

    Although it seems much more reasonable to implement the comparison
    operations as free functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
Suppose I have an entity object defined as public partial class Article { public
Suppose I have a method which changes the state of an object, and fires
Suppose I have a class module clsMyClass with an object as a member variable.
Here's my situation.. Suppose you have the following model entities, which represent single tables
Suppose I have a composite class PharmaProduct (which represents the product hierarchy of a
Suppose you have an object which can be accesed by many threads. A critical
Suppose I have a Javascript object which is initialized var letters = {q:0, t:0,
Suppose I have a simple Java class like this: public class User { String
Suppose I have an object in PHP $o, about which I can get $o->a->b,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.