Detailed explanation of function calls and subscripts in C + + and overloaded _c language for member access operators

Source: Internet
Author: User

Function call
the function call operator called with parentheses is a two-dollar operator.
Grammar

Primary-expression (Expression-list)

Note
In this context, primary-expression is the first operand, and expression-list (possibly an empty list of parameters) is the second operand. The function call operator is used for operations that require a large number of parameters. This works because Expression-list is a list rather than a single operand. Function call operators must be non-static member functions.
The function call operator does not modify how the function is invoked when it is overloaded; instead, it modifies how the operator is interpreted when the operator is applied to an object of the type of the given class. For example, the following code usually has no meaning:

Point pt;
PT (3, 2);

However, if there is an appropriate overloaded function call operator, this syntax can be used to offset the X coordinate to 3 units and offset the Y coordinate to 2 units. The following code shows such a definition:

Function_call.cpp
class Point
{public
: Point
  () {_x = _y = 0;}
  Point &operator (int dx, int dy)
    {_x + = dx; _y = dy; return *this;}
Private:
  int _x, _y;

int main ()
{point
  pt;
  PT (3, 2);
}

Note that the function call operator applies to the name of the object, not the name of the function.
You can also overload a function call operator by using a pointer to a function, rather than the function itself.

typedef void (*PTF) ();
void func ()
{
}
struct S
{
  operator PTF ()
  {return
   func;
  }
};

int main ()
{
  s S;
  S ();//operates as S.operator PTF () ()
}

Subscript
the subscript operator ([]), such as the function call operator, is treated as a two-dollar operator. The subscript operator must be a non-static member function that takes a single parameter. This parameter can be of any type and specify the desired array subscript.
The following example shows how to create a vector of type int that implements bounds checking:

Subscripting.cpp//compile with:/EHsc #include <iostream> using namespace std;
  Class Intvector {public:intvector (int celements);
  ~intvector () {delete [] _ielements}
int& operator[] (int nsubscript);
  Private:int *_ielements;
int _iupperbound;

};
Construct an intvector.
  Intvector::intvector (int celements) {_ielements = new int[celements];
_iupperbound = celements;
}//subscript operator for Intvector.

  int& intvector::operator[] (int nsubscript) {static int iErr =-1;
  if (nsubscript >= 0 && nsubscript < _iupperbound) return _ielements[nsubscript];
   else {clog << "Array bounds violation." << Endl;
  return IERR;
}//Test the Intvector class.
  int main () {Intvector V (10);

  int i;

  for (i = 0; I <= ++i) v[i] = i;

  V[3] = v[9];
for (i = 0; I <= ++i) cout << "Element: [" << i << "] =" << v[i] << Endl;}
Array bounds violation. ELement: [0] = 0 element: [1] = 1 element: [2] = 2 element: [3] = 9 element: [4] = 4 element: [5] = 5 element: [6] = 6 Elem
ENT: [7] = 7 element: [8] = 8 element: [9] = 9 Array bounds violation.

 Element: [10] = 10

Comments
When I reaches 10 in the previous program, operator[] detects if the subscript is being used beyond the bounds and emits an error message.
Note that the function operator[] returns the reference type. This makes it a left value, allowing you to use subscript expressions on either side of the assignment operator.

Member access
class member Access can be controlled by overloading the member access operator (–>). This operator is considered a unary operator in this usage, and the overloaded operator function must be a class member function. Therefore, the declaration of such a function is:
Grammar

Class-type *operator–> ()

Note
Where Class-type is the name of the class to which this operator belongs. member access operator functions must be non-static member functions.
This operator, which is commonly used with the pointer dereference operator, is used to implement the smart pointer that validates the pointer before dereferencing the usage or counting the usage.
Cannot overload. The member access operator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.