So, I know that this code works, but I have several questions.
1) Is is practical to have multiple classes reference to a single variable or class entity ?
2) When writing a multithreading program, will this technique break?
3) is there a smarter or better approach, than this simple code?
Code:
#include <iostream>
using namespace std;
class Increase
{
int & numb;
public:
Increase( int & n ) : numb(n) { }
void by( unsigned int val) { numb += val; }
};
class Decrease
{
int & numb;
public:
Decrease( int & n) : numb(n) { }
void by( unsigned int val) { numb -= val; }
};
int main(void)
{
int number = 20;
Increase inc(number);
Decrease dec(number);
inc.by(20);
cout<< number << "\n";
dec.by(10);
cout<< number <<"\n";
return 0;
}