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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:09:46+00:00 2026-05-17T23:09:46+00:00

I am looking for some help regarding the pointer dereference operator -> . Let

  • 0

I am looking for some help regarding the pointer dereference operator -> . Let me describe what I am trying to do.

I am implementing a unidirectional iterator for a special container. The container is special in the sense that it does not physically allocate any space for the contained values but generates them at run time on demand. For example, consider that the container is “M consecutive integral multiples of N“.

Since I do not want to store the value directly in my iterator, I create a value on the heap on demand.

When I need a pointer to the value I delete the old one if it’s out-of-date and create a new one. This means that invocation of operator *() or operator ->() may delete an old value and new a new value, if the iterator has been advanced with operator ++() after they were last invoked.

Now I would like to use a smart_ptr to point at my value rather than keep a native pointer around. In order to do so I realize I need to understand the semantics of the -> operator better.

  • First of all, is -> a unary operator ?
  • If that be so, how does i->member work. This would translate to (pointer returned)member, which is not a syntactically valid form.
  • “member” could be a data member or a member function.
  • ->() smells more like a binary operator that executes (*pointer returned).member. Since “member” is not a value, such a semantics is not equivalent to a binary operator either.
  • What happens to the pointer returned by ->() ? who is supposed to own it ?
  • How can I use RAAI in this framework ? Is reference counted pointers the only option ?
  • There is no -- operator for this iterator so i don’t need to keep the previous values around

Thanks for your responses. Ending with a meta question, should this be a wiki ?

  • 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-17T23:09:47+00:00Added an answer on May 17, 2026 at 11:09 pm

    Here is the answer to answer your questions, though I do not think they are so useful as you might hope:

    • Yes, it is a unary operator
    • No, it does not care what member is. If member isn’t a field/member of the class/struct pointed to be the return of operator -> then the compiler will complain.
    • member could indeed be either a data member or a member function.
    • No, it’s a unary operator since it does nothing whatsoever with member. It’s there just to implement smart pointers. It is perfectly valid to say return_type *ptr = smarptr.operator ->().
    • It’s owned by whatever returns it. But this is purely by convention. You can decide whatever you want. But if you decide anything other than that it will surprise a whole ton of people to the point they will think they have a bug in their program and will never think to wonder if your smart pointer implementation has some bizarre semantics nobody else uses.
    • If you use operator -> for anything at all other than implementing a smart pointer I have no intentions of helping you write code that programmers after you will revile and make fun of.
    • This is a statement, not a question, so I have no answer for it. 🙂

    I question your desire to point at your value. It seems to me like you could hold it by value as a member value of your iterator, and I will give an example of how this could work below. But if you’re set on using a pointer to your value, and want to use a smart pointer, just return the result of smartptr.operator ->() for your own operator ->(). You might also use the member function of your smart pointer (often get) that returns a ‘bare’ pointer and that would likely confuse people a little less.

    Here is a sample of how your example container should work:

    class multiples {
      public:
       multiples(int n, int starting_multiplier, int ending_multiplier)
          : n_(n), starting_(starting_multiplier), ending_(ending_multiplier)
       {
       }
    
       class const_iterator {
          friend class multiples;
         public:
          const int &operator *() const { return curval_; }
          const int *operator ->() const { return &curval_; }
    
          const const_iterator &operator ++() { curval_ += n_; return *this; }
          const const_iterator operator ++(int) {
             const_iterator tmp(*this);
             curval += n_;
             return tmp;
          }
    
          bool operator ==(const const_iterator &b) const { return curval_ == b.curval_; }
          bool operator !=(const const_iterator &b) const { return curval_ != b.curval_; }
    
         protected:
          explicit const_iterator(int n, int starting) : n_(n), curval_(starting * n) {}
    
         private:
          const int n_;
          int curval_;
       };
    
       const_iterator begin() const { return const_iterator(n_, starting_); }
       const_iterator end() const { return const_iterator(n_, ending_); }
    
      private:
       const int n_, starting_, ending_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for some help with a Labview data collection program. If I could collect
Hi I'm looking for some help in mapping the following tables to a hibernate
I'm looking for some help on my discussion. We're discussing two solutions to a
I'm looking for some help with a dependency issue. In a nutshell, I've included
HI, I am new to the scrum methodology and looking for some help to
I'm looking for a tutorial, blog entry, or some help on the technique behind
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Looking for some direction here as I'm running into some migration problems. We have
Looking for some sample code (C#) for a simple thread pool implementation. I found
Looking at some code I'm maintaining in System Verilog I see some signals that

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.