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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:18:19+00:00 2026-06-01T14:18:19+00:00

What I am trying to do, is return a ForwardIterator(or even a pair of

  • 0

What I am trying to do, is return a ForwardIterator(or even a pair of begin and end iterators), so that the uderlying implementation can be kept hidden from a client class.
I was not able find any such example.

if underlying implementation is vector, the iterator is vector::iterator.. Even if you templatize the output , this does not seem to work.

  • 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-01T14:18:21+00:00Added an answer on June 1, 2026 at 2:18 pm

    You can’t simultaneously return an object as is and expect the caller not to know its type. The typical way to “hide” the real type of any object is to hide it behind an interface.

    For example, you might want to write something like this:

    template<typename T>
    class Cursor
    {
    public:
        typedef T value_type;
        virtual ~Cursor () {}
        virtual bool has_result () = 0;
        virtual value_type get_result () = 0;
    };
    
    // implements cursor interface for any sequence
    // described as a pair of forward iterators.
    template<typename I>
    class ForwardSequenceCursor :
        public Cursor<std::iterator_traits<I>::value_type>
    {
        I myCurrent;
        const I myEnd;
    public:
         ForwardSequenceCursor(I begin, I end)
             : myCurrent(current), myEnd(end)
         {}
         virtual bool has_result () {
             return myCurrent != myEnd;
         }
         virtual value_type get_result () {
             return *myCurrent++;
         }
    };
    

    Then, you can return any forward sequence as:

    class Foo
    {
        std::vector<int> myValues;
    public:
        std::unique_ptr< Cursor<int> > values () {
            return std::unique_ptr< Cursor<int> >(
                new ForwardSequenceCursor<vector<int>::const_iterator>(
                    myValues.begin(), myValues.end()
                )
            );
        }
    };
    

    And use it like so:

    std::unique_ptr< Cursor<int> > cursor = foo.values();
    while (cursor->has_result()) {
        std::cout << cursor->get_result() << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to return the value that a $ajax call returns, from a function
I am trying to return an IEnumerable from a method that is backed by
When trying to return the path to system32 so I can launch notepad from
I'm trying to return attributes of columns from my DataContext. How can I pull
im trying to return a string value from a method inside my script tag
Im trying to return a SimpleQuery list that queries a single table and uses
I'm trying to return a dictionary from a function. I believe the function is
I'm trying to return a transparent GIF from an .aspx page for display within
I am trying to return an array Data Member from one smaller 2D Array
I am trying to return the result that I found in my query 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.