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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:33:30+00:00 2026-06-07T05:33:30+00:00

I have an iterator which iterates over a std::shared_ptr. So operator++ points the internally

  • 0

I have an iterator which iterates over a std::shared_ptr. So operator++ points the internally stored shared pointer to the next item.

template<class IT>
class MyIterator
{
public:
    ...

    MyIterator& operator++()
    {
        _i = ... // Call factory
        return *this;
    }
private:
    std::shared_ptr<IT> _i;
};

How should I implement the operator*() and operator->() operators?

How should I test if the iterator is pointing to NULL, i.e. if the internal shared pointer is pointing to NULL.

  • 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-07T05:33:31+00:00Added an answer on June 7, 2026 at 5:33 am

    You should define under what circumstances users are permitted to deference an instance of your class. Usually this is “anything other than an end iterator or an uninitialized iterator”.

    Then you should ensure that _i never contains a null pointer for an instance that can be dereferenced.

    This means there is no need for a check, because the user is not permitted to call operator* or operator-> in that circumstance. You could add one for debugging, for example: assert(_i.get());.

    You don’t specify what the value_type is of your iterator, but assuming that it is IT, you can implement:

    IT &operator*() { return *_i; }
    shared_ptr<IT> operator->() { return _i; }
    // or
    IT *operator->() { return _i.get(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Map over which I iterate like this: std::map<unsigned int, GameObject *>::iterator
I have a Abstract Iterator class which has this function void iterate(){ while(this.hasnext()){ ..this.next()..
I have a custom iterator (TokenIterator to be precise, which iterates, well, tokenized php
Suppose I have a function which can either take an iterable/iterator or a non-iterable
I have a large set of data which I access via a generator/iterator. While
I have a custom container class for which I'd like to write the iterator
I have the following function which creates a std::vector of iterators into another container:
I have a custom iterator template class that wraps up a std::list iterator. In
I have a row-major iterator to a 2D array, with derefence operator as follows:
Is there a simple or standard way to have a multimap iterator which iterate

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.