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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:17:38+00:00 2026-05-27T22:17:38+00:00

Well I’m currently refactoring a class I made quite a long time ago. The

  • 0

Well I’m currently refactoring a class I made quite a long time ago. The class is a container type.

Many functions can use advantage of the class structure, and are thus implemented as member functions. However it now seems to be a lot of function which seem “the same”, ie a “find” function:

iterator find(ITEM)
const_iterator find(ITEM) const;
iterator find_if(ITEM, PRED)
const_iterator find_if(ITEM, PRED) const;

4 “functions” to describe nearly the same (and code is almost the same in each version). This becomes very tedious when updating the class, I have to make sure each version is upgraded. Is there a manner to handle these things better? Some other function in the class CAN possibly take 2 predicates, and that meant I suddenly had 8 functions to manage.

I tried calling the “non-constant version from the constant version” but that obviously doesn’t work.

So how does one handle these things? Just bite the bullet and write it down?

EDIT: just to inform: my datastructure resembles a “tree”. Each “object” contains the data (which the find searches) and a “list” with sub-trees. The find function works recursive over all subtrees of a tree (and sub-sub trees). – Just like one would expect when searching a tree.

As there isn’t a clear “end” or “start” iterator to such a tree, using std::find doesn’t yield the correct functionality.

  • 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-27T22:17:39+00:00Added an answer on May 27, 2026 at 10:17 pm

    Scott Meyers solved a similar problem in Item 3 of Effective C++ by calling the const version inside the non-const version and then casting the const result back to non-const:

    const_iterator find(const T& value) const
    {
        // actual implementation of find
    }
    
    iterator find(const T& value)
    {
        return const_cast<iterator>(static_cast<const container*>(this)->find(value));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, after a long time writing .net programs in C# I started to feel
Well, not random, because its the same every time, but #include<iostream> using namespace std;
Well I'm doing some Java - C integration, and throught C library werid type
Well, I have a class Customer (no base class). I need to cast from
Well, I am storing variables inside of a class that will be used for
Well normally I am quite good at figuring and researching problems without guidance however
Well, I have to revive a question that was answered here before. I've made
well. i was use Reactive Extensions (Rx) package async Request,Before you can start writing
Well, it seems simple enough, but I can't find a way to add a
Well i currently want to do a search engine with SQL and PHP. I

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.