Created
April 12, 2018 11:32
-
-
Save vakho10/774bf324608fccfb88986e9e46d20fd7 to your computer and use it in GitHub Desktop.
C++ operator overloading example overloading [][] brackets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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