Details the overloading of function calls and subscripts and member access operators in C++

  • 2020-05-07 20:13:55
  • OfStack

function call
The function call operator called by
with parentheses is the 2-meta operator.
grammar


primary-expression ( expression-list )

note
In this context, primary-expression is the first operand expression-list (an empty list of possible 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 1 operand. The function call operator must be a non-static member function.
The function call operator does not change the way the function is called when overloaded. Instead, it changes the way an operator is interpreted when it is applied to an object of type of a given class. For example, the following code is often meaningless:


Point pt;
pt( 3, 2 );

However, if there is an appropriate overloaded function call operator, this syntax can be used to offset the x coordinates by 3 units and the y coordinates by 2 units. The following code shows the 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 the 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 2 meta operator. The subscript operator must be a non-static member function that takes a single argument. This parameter can be of any type and specifies the desired array index.
The following example demonstrates how to create a vector of type int for boundary 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 <= 10; ++i )
   v[i] = i;

  v[3] = v[9];

  for ( i = 0; i <= 10; ++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
Element: [7] = 7
Element: [8] = 8
Element: [9] = 9
Array bounds violation.
Element: [10] = 10

annotation
When i reaches 10 in the previous program, operator[] detects whether an out-of-bounds subscript is being used and sends an error message.
Note that the function operator[] returns the reference type. This makes it an lvalue, which allows you to use subscript expressions on any 1 side of the assignment operator.

member access
class member access can be accessed by overloading the member access operator (wok) > ) to control. This operator is treated as a 1 meta operator in this usage, and an 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 (typically used in conjunction with pointer dereference operator 1) is used to implement a "smart pointer" that validates the pointer before dereferencing or counting against the usage.
The.member access operator could not be overridden.


Related articles: