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 8061215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:15:48+00:00 2026-06-05T10:15:48+00:00

I have a problem which seems simple, but I can’t find an elegant solution

  • 0

I have a problem which seems simple, but I can’t find an elegant solution for it.

I’m using a linked list which I need to sort twice:

  • once on one parameter as list elements are added, and
  • once on a different parameter after some processing.

The objects I store in the list inherit from an abstract list item base class.

The problem is with resorting on the second parameter. I don’t want to write a pure virtual accessor for it in the list item base class, because I later store some other objects in the list which also inherit from the list item base class, but the second accessor wouldn’t make sense for them.

Is there a clean solution that I’m missing?

  • 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-06-05T10:15:50+00:00Added an answer on June 5, 2026 at 10:15 am

    If I understand correctly, you’ve something like that :

     class Base {
        public:
        int getParam1() const { ... }; 
        virtual void foo() =0;  // this is an abstract class
        virtual ~Base() { ... }
     }
     class Derived : public Base {
        public:
        int getParam1() const { ... }
        int getParam2() const { ... }
    
     }
    

    And you have a std::list<Base*> myList somewhere. The first time you want to sort your list, you do something like :

    sort(list.begin(),list.end(),[](const Base*v1,const Base*v2) {
        return v1->getParam1()<v2->getParam1();
    });
    

    And for the second sort, you need to use something specific to the class Derived. You don’t want to add a pure virtual accessor getParam2() into Base & implement it in Derived. But if you know that your list contains only Derived objets, you can use a cast without problem :

    sort(list.begin(),list.end(),[](const Base*b1,const Base*b2) {
        const Derived * d1 = dynamic_cast<const Derived*>(b1);
        const Derived * d2 = dynamic_cast<const Derived*>(b2);
    
        return d1->getParam2() < d2->getParam2();
    });
    

    Note that if an object of your list is not a Derived, dynamic_cast will return nullptr so you can safely check it.

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

Sidebar

Related Questions

I have a problem which i can't seem to find the solution to. I
It looks like a problem which could have simple solution, but I haven't found
my problem seems simple but I can not manage to find an answer here.
This should be simple but I can't find a solution: I have a custom
I have a strange problem which probably has a very simple solution, I just
I have what seems at first blush to be a simple problem, but I
I have a code problem which stems from the fact that I am using
I have a problem which I cant seem to find answer to through searches
I have a strange problem which I can't fix: A field: private boolean[][][] gaps;
i have a small problem here which i cannot fix,This post goes through but

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.