dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 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.

 

 

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