devxlogo

What to do with overload arguments

What to do with overload arguments

Question:
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 -> ?

Answer:
First, the . operator cannot be overloaded. If it could be, there would be no way to access members of the objectdirectly.

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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist