0

I am writing my own iterator in C++:

class my_iterator {
  entity operator*() {
    ...
  }
  my_iterator& operator++() {
    ...
  }
}

I can dereference an entity using the * operator. However, can I let my custom iterator support the -> operation (followed by some property or method of a dereferenced entity)?

Is there a certain operator I can implement to support ->?

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • Uhm... `operator->`? Check out [this question](http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719) right here on StackOverfow! – Nik Bougalis Apr 09 '13 at 05:19
  • Oh! Thanks! I wasn't sure because `->` is followed by some property name. – dangerChihuahua007 Apr 09 '13 at 05:21
  • Your `operator *` returns a new object. Is your iterator meant to be a constructing iterator? – Zacrath Apr 09 '13 at 05:23
  • Oh woops! I changed that. I was just trying to find out the general method of how to implement `->`. Based on the answers below, should I just have `operator->` return say a shared pointer of an entity? I can't just return a normal pointer because it'll be invalid after the stack returns, right? – dangerChihuahua007 Apr 09 '13 at 05:25
  • Wait, you're right. I changed it back sorry. I am constructing a new object in `operator*`. :/ – dangerChihuahua007 Apr 09 '13 at 05:26

4 Answers4

2

Yes, you overload -> if you want special behavior, otherwise you get it with it's standard behavior on pointers.

For your case you'll have something like

entity* operator->() {
    return ptr_to_entity;
}

This is slightly odd because with -> your overload returns a pointer to an object and then -> is used on that.

Eg the above makes:

my_iterator_instance->foo === ptr_to_entity->foo
daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
1

Yes it's called operator->. But remember operator-> is special, it should return a pointer and the regular operator-> will be applied to that pointer. So given the code above I can't tell you exactly how to implement it. If you fill out the details of your operator* I might be able to give more help.

Probably something along these lines.

entity* operator->() {
    ...
}
john
  • 85,011
  • 4
  • 57
  • 81
1

Check out this answer: Common operators to overload. You can skip to "Operators for Pointer-like Types" to find what you want.

Community
  • 1
  • 1
Daniel Dinu
  • 1,783
  • 12
  • 16
1

An extract from Operator overloading

I strongly advise you to read the above as its well written

  Operators for Pointer-like Types

For defining your own iterators or smart pointers, you have to overload the unary prefix > dereference operator * and the binary infix pointer member access operator ->:

class my_ptr {
    value_type& operator*();
    const value_type& operator*() const;
    value_type* operator->();
    const value_type* operator->() const;
  };

Note that these, too, will almost always need both a const and a non-const version. For >the -> operator value_type must be of class (or struct or union) type, otherwise their >implementation results in a compile-time error.

The unary address-of operator should never be overloaded.

For operator->*() see this question. It's rarely used and thus rarely ever overloaded. In >fact, even iterators do not overload it.

Community
  • 1
  • 1
Pradheep
  • 3,553
  • 1
  • 27
  • 35