1

Possible Duplicate:
Operator overloading

The member access operator -> can be overloaded to return a pointer to a data member, or some other variable.

Where is this feature used ? What coding problems does it solve or alternately, what does it make easier ?

Community
  • 1
  • 1
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • usually you want to override an operator, I still can't find one example about overloading an operator at all, are you sure about this ? – user1802174 Nov 18 '12 at 13:25
  • @Mat I usually call this "overriding" not "overloading" – user1802174 Nov 18 '12 at 13:29
  • 2
    @user1802174 Nope, the terminology is indeed *overloading*, not overriding. If you call it differently then you are doing it wrong (i.e. different from all other people). “Overriding” is reserved for virtual methods which are redefined in derived classes. This terminology is common to most OOP languages, including C++. – Konrad Rudolph Nov 18 '12 at 13:30
  • 1
    @user1802174 overriding is the expression used for redefining a member function in a subclass. Overloading is specifically used for operators. –  Nov 18 '12 at 13:30
  • @KonradRudolph in the paragraph "Operators for Pointer-like Types" the signature doesn't change, the behaviour does, this is about redefining a new behaviour, how can be considered an overload ? – user1802174 Nov 18 '12 at 13:31
  • @user1802174 See updated comment. And in fact, the signature *does* change. – Konrad Rudolph Nov 18 '12 at 13:31
  • @H2CO3 I'm used to the concept that an overload it's a change of signature, an override it's a change about the body or the definition. – user1802174 Nov 18 '12 at 13:31
  • @KonradRudolph well since 3 of you are calling this with the same name I suppose that I'm the one who is wrong, but really every books about OOP languages that i have read defines overload and override in this way. Probably something is different for the operators. – user1802174 Nov 18 '12 at 13:35
  • @user1802174 No, in fact the definitions are always the same. You are simply assuming (wrongly) that the signature of overloaded operators stays the same, but it simply doesn’t. `operator->` is a bit of a special case because the *visible* signature is indeed the same but if you look at the hidden `this` parameter you’ll see that this changes (and although this is the same for overriding, here the change is within a class hierarchy only). – Konrad Rudolph Nov 18 '12 at 13:38
  • @KonradRudolph I haven't looked at the returned value because according to what i know it's not part of the signature, the signature it's only about what is declared in the signature, the first row of every method/function. The returned value it's part of the body, meaning that it's part of the definition, and if I change the definition it's an override not an overload. I will read the entire answer to this and that question, I'm really curious about this now. – user1802174 Nov 18 '12 at 13:49
  • 1
    @user1802174 I’m not talking about the returned value – you’re right that it’s not part of the signature in C++. I’m talking about the type of `this` (which *is* part of the signature, even though it’s not written explicitly when declaring a function inside a class). – Konrad Rudolph Nov 18 '12 at 13:54
  • To be fair, operator overloading _does_ better fit the name "hiding", and it's closer to "overriding" without the virtual, since the signatures don't change. It's a poor term in that regard, but it's the extant one. – Lightness Races in Orbit Nov 18 '12 at 15:14
  • How is hiding a better term ? @LightnessRacesinOrbit You are (overall) increasing the functionality of the operator not reducing it or changing it – asheeshr Nov 18 '12 at 15:44
  • @AshRj: It hides the operator function with the same signature in a base class. The very definition of name hiding in C++. I don't care about some wishy-washy OO-speak definition. Though if we're going to include `this` then, yes, it's overloading. I haven't seen `this` included in such discussions in any other context, though. – Lightness Races in Orbit Nov 18 '12 at 15:45

3 Answers3

4

The member access operator is a somewhat odd creature: It is meant to return a pointer or a class with a member access operator overloaded. Once it reaches a pointer it just accesses the corresponding member. The primary use of overloading the member access operator are smart pointers, e.g., std::shared_ptr<T> and std::unique_ptr<T>. Without this operator you'd need to use something like

sp.get()->member

or

(*sp).member

instead of

sp->member
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

Note that both terms "overriding" and "overloading" are greatly misleading. The canonical -> operator accesses the member of an object referenced through a pointer, that is X* x; x->foo; is accessing something pointed to by x which is of a pointer type (or to be more precise a raw pointer).

However, the operator-> that you can implement as a non-static member function in aggregate types (i.e. "classes") does something different. In X* x; x->foo;, -> would still be the canonical structure operator, which cannot be changed. However, in Y y; y->foo, -> would invoke the operator-> member function of Y. This seemingly small distinction is crucial as one operator can only be applied to raw pointer types and the other can only be applied to non-pointer types.

This is typically used to allow types to behave syntactically as-if they were raw pointers (with some semantic differences), like in shared_ptr et al. This could not be achieved without this language support as shared_ptr<X> and X* could not be used in the same fashion if there was no shared_ptr<X>::operator-> allowing to mimic the canonical -> operator that is applicable to X* (but not X).

bitmask
  • 32,434
  • 14
  • 99
  • 159
0

When you're modeling a pointer, and you want to maintain the usual syntax for convenience. Just take a look at std::unique_ptr and std::shared_ptr. :)

cooky451
  • 3,460
  • 1
  • 21
  • 39