Skip to content

Instantly share code, notes, and snippets.

@andiac
Last active May 25, 2020 19:09
Show Gist options
  • Save andiac/2cff630eef907e67d42d8714f463188d to your computer and use it in GitHub Desktop.
Save andiac/2cff630eef907e67d42d8714f463188d to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2020-2021 Andi Zhang <me@andi.ac>
*/
#include <iostream>
using namespace std;
class MyInt {
public:
int val;
int* bindInt;
MyInt(int val, int* bindInt) {
this -> val = val;
this -> bindInt = bindInt;
if(this -> bindInt != NULL)
*(this -> bindInt) = -val;
}
void operator=(int val) {
this -> val = val;
if(this -> bindInt != NULL)
*(this -> bindInt) = -val;
}
friend ostream& operator<<(ostream& out, const MyInt& a) {
out << a.val;
return out;
}
};
class DoubleInt {
public:
MyInt a = MyInt(0, NULL);
MyInt b = MyInt(0, NULL);
DoubleInt(int val = 0) {
(this -> a).bindInt = &(this -> b).val;
(this -> b).bindInt = &(this -> a).val;
this -> a = val;
}
};
int main() {
DoubleInt ab = DoubleInt();
MyInt &a = ab.a;
MyInt &b = ab.b;
a = 8;
cout << "a: " << a << ", b: " << b << endl;
b = 666;
cout << "a: " << a << ", b: " << b << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment