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

  • Home
  • SEARCH
  • 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 405301
In Process

The Archive Base Latest Questions

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

I have a class which I want to expose a list of structs (which

  • 0

I have a class which I want to expose a list of structs (which just contain some integers).
I don’t want the outside to modify these data, just iterate over it and read them
Example:

struct TestData
{
  int x;
  int y;
  // other data as well
}

class IterableTest
{
  public:
    // expose TestData here
};

now in my code I want to use my class like this:

IterableTest test;
BOOST_FOREACH(const TestData& data, test.data())
{
  // do something with data
}

I’ve already read this article http://accu.org/index.php/journals/1527 about memberspaces.
However, I don’t want to (or can’t) save all TestData in an internal vector or something.
This is because the class itself doesn’t own the storage, i.e. there is actually no underlying container which can be accessed directly by the class. The class itself can query an external component to get the next, previous or ith element, though.

So basically I want my class to behave as if it had a collection, but in fact it doesn’t have one.
Any ideas?

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

    It sounds like you have to write your own iterators.

    The Boost.Iterator library has a number of helpful templates. I’ve used their Iterator Facade base class a couple of times, and it’s nice and easy to define your own iterators using it.

    But even without it, iterators aren’t rocket science. They just have to expose the right operators and typedefs. In your case, they’re just going to be wrappers around the query function they have to call when they’re incremented.

    Once you have defined an iterator class, you just have to add begin() and end() member functions to your class.

    It sounds like the basic idea is going to have to be to call your query function when the iterator is incremented, to get the next value.
    And dereference should then return the value retrieved from the last query call.

    It may help to take a look at the standard library stream_iterators for some of the semantics, since they also have to work around some fishy “we don’t really have a container, and we can’t create iterators pointing anywhere other than at the current stream position” issues.

    For example, assuming you need to call a query() function which returns NULL when you’ve reached the end of the sequence, creating an “end-iterator” is going to be tricky. But really, all you need is to define equality so that “iterators are equal if they both store NULL as their cached value”. So initialize the “end” iterator with NULL.

    It may help to look up the required semantics for input iterators, or if you’re reading the documentation for Boost.Iterator, for single-pass iterators specifically. You probably won’t be able to create multipass iterators. So look up exactly what behavior is required for a single-pass iterator, and stick to that.

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

Sidebar

Related Questions

I have a class library, which I want to expose to the outside world
I have a class in my library which I want to expose to the
I have an ICollection<T> called foos in my class which I want to expose
I have a class which I want to add a property with using formula
I have this class which I want to pass around Windows as LPARAM parameter.
I have a custom class which I want to load inside the firstViewController and
So I have this class which extends an activity. But I want to draw
I have a class Test which is in lib folder and I want to
I want to have a base class which dictates the alignment of the objects
I have a class called Trial which has_many results. Now What I want to

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.