7

I recently view the source code of SGI STL. I want to know whether I can use the "->" operator to replace the (*node).data to implement the operator*(), like this:

reference operator*() const {return (*node).data;}

replaced by:

reference operator*() const {return node->data;}

in addition:

node is a pointer which points to a struct object, like this:

template<class T>
struct __list_node {
    typedef void * void_pointer;
    void_pointer prev;
    void_pointer next;
    T data;
};
4
  • You can, but what's the point? Commented Dec 22, 2013 at 14:44
  • 2
    Probably, yes. What type is node and does it overload operator-> or unary operator*? Commented Dec 22, 2013 at 14:44
  • node is a pointer which points to a struct __list_node {} Commented Dec 22, 2013 at 14:47
  • @herohuyongtao They usually should be, but nothing enforces that. Just like ++a and a++ could affect a in different ways – or just like c << n could have side-effects and mean something different from bit-wise shifting, to name the ever-popular early overload changing an operator meaning. Commented Dec 26, 2013 at 13:34

1 Answer 1

9

In most cases (for example, when node is a pointer), these will be equivalent. The x->y operator is defined as being equivalent to (*(x)).y. However, it's possible to overload operator* or operator->, in which case they may not behave as you would expect (but they should).

As you have said, in this case node is just a pointer, so they are equivalent.

Sign up to request clarification or add additional context in comments.

1 Comment

"in which case they may not behave as you would expect" -- But if they don't, please stop using that library. The author is a bad person.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.