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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:14+00:00 2026-05-16T17:06:14+00:00

I’ll try and keep my sample code very straightforward, but it may have errors

  • 0

I’ll try and keep my sample code very straightforward, but it may have errors as I’m typing it on the spot.

I have a class named Phone.

class Phone
{
 public:
  Phone(std::string manufacturer, std::string model, std::vector<Feature> features);

 private:
  std::vector<Features> features;
  std::string model;
  std::string manufacturer;
};

I have a struct named Feature.

struct Feature
{
   Feature(std::string feature, std::string category);
   std::string feature;
   std::string category;
};

As you can see a phone has a list (vector) of features: ie. bluetooth, GPS, radio, etc, which have a category: networking, navigation, multimedia.

Now information about phones and features are stored in a sqlite3 database.
I have a helper function which will retrieve a particular phone model from the database and return a populated Phone object.
I also have a function which takes in a Phone object and writes the Phone to the database.

The problem is I need to provide the client some way of iterating over the list of Features of a Phone. For starters the database helper needs to know about the features so it can write them to the database. Second the client may need to retrieve a Phone from the database and then display the list of features to the user.

One solution is to add the following functions in class Phone

std::vector<Feature>::iterator begin()
std::vector<Feature>::iterator end()

This is not an ideal solution for me because the client code will not look intuitive – it will appear as if the client is iterating over a Phone, when they are actually iterating over Features.

Another solution is discussed in an article at http://accu.org/index.php/journals/1527 which discusses using a technique named “memberspaces” to expose iterators to client code. It would result in more readable client code, but the implementation is a bit messy in my opinion.

Is there a better solution to the iterator problem, or maybe there is a more suitable design pattern I could be using.

Any feedback would be greatly appreciated.

  • 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-16T17:06:15+00:00Added an answer on May 16, 2026 at 5:06 pm

    In your case I would go for better names first:

    typedef std::vector<Feature> Features;
    
    Features::iterator features_begin();
    Features::iterator features_end();
    Features::const_iterator features_begin() const;
    Features::const_iterator features_end() const;
    

    Examples:
    1)

     // Note: you'll need to define an operator<< for Feature
     // can be used with std::algorithms
     std::copy( phone.features_begin(), phone.features_end(),
       std::ostream_iterator<Feature>( std::cout, "\n\r" ) );     
    

    2)

    // Note: shamelessly borrowed from http://www.boost.org/doc/libs/1_44_0/doc/html/foreach/extensibility.html
    // add overloads of range_begin() and range_end() for Phone::Features
    inline Phone::Features::iterator range_begin( Phone& phone ){
       return phone.features_begin();
    }
    
    inline Phone::Features::iterator range_end( Phone& phone ){
       return phone.features_end();
    }
    
    namespace boost{
       // specialize range_mutable_iterator and range_const_iterator in namespace boost
       template<>
       struct range_mutable_iterator< Phone >{
          typedef Phone::Features::iterator type;
       };
    
       template<>
       struct range_const_iterator< Phone >{
          typedef Phone::Features::const_iterator type;
       };
    }
    ...
    // can be used with BOOST_FOREACH
    BOOST_FOREACH( Feature x, phone ){
       std::cout << x << std::endl;
    }
    

    P.S. Given Jonannes’ suggestion and the naming convention used by boost::range the names are now features_xxx() instead of xxx_features() (especially since they do make more sense in this context).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.