Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Jul 18, 1997

What to do with overload arguments

Which arguments do you receive when you overload . and -> , and what do you do with the arguments? I know that for overloading [] you receive the index. How about . and -> ?

First, the . operator cannot be overloaded. If it could be, there would be no way to access members of the object directly.

Here are the rules for operator -> :

  1. Operator -> must be defined as a member of a class.
  2. The operator takes a second argument but the type is unspecified.
  3. The return value must be a type for which operator -> is meaningful.

Here is an example:

struct X
   Y * operator ->() { return yPtr_; }

   struct Y { void foo (); }
   Y * pPtr;

X x; x->foo () ; // calls X::Y::foo
The operand to operator -> (in this case foo) is applied to its return value. If the return value is something for which -> does not make sense, that is an error.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date