User Tools

Site Tools


c_-_c_operator_overloading:overloading_square_bracket

C - C++ Operator Overloading - Overloading Square Bracket

#include <iostream>
using namespace std;
 
class Point 
{
  int a[3];
 
public:
  Point(int i, int j, int k) 
  {
    a[0] = i;
    a[1] = j;
    a[2] = k;
  }
 
  int operator[](int i) { return a[i]; }
};
 
 
int main()
{
  Point ob(1, 2, 3);
 
  cout << ob[1]; // displays 2
 
  return 0;
}
c_-_c_operator_overloading/overloading_square_bracket.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki