Skip to content

Instantly share code, notes, and snippets.

@vakho10
Created April 12, 2018 11:32
Show Gist options
  • Save vakho10/774bf324608fccfb88986e9e46d20fd7 to your computer and use it in GitHub Desktop.
Save vakho10/774bf324608fccfb88986e9e46d20fd7 to your computer and use it in GitHub Desktop.
C++ operator overloading example overloading [][] brackets.
#include <iostream>
class DoubleArray
{
public:
double** arr;
DoubleArray(double** arr) : arr(arr) {}
class Proxy
{
public:
double* _val;
Proxy(double* _val) : _val(_val) { };
int operator[](int index) {
return _val[index];
}
};
Proxy operator[](int index) {
return Proxy(arr[index]);
}
void operator=(double** arr) {
this->arr = arr;
}
};
int main()
{
using namespace std;
double** twoDim = new double*[3] {
new double[3] { 1, 2, 3 },
new double[2] { 3, 4 },
new double[4] { 5, 6, 7 },
};
DoubleArray jnz = twoDim;
cout << jnz[0][2] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment