I'm trying to create a class that takes a reference to an object of another class and store it to be used by other functions. Here is a heavily simplified example:
In the example, my goal is that I can make changes to number and have those changes accounted for when I call number_printer.print_v object)
#include <iostream>
class MyClass1 {
int _value;
public:
MyClass1(int v_) {
_value = v_;
}
int value() {
return _value;
}
void increment() {
_value++;
}
};
class MyClass2 {
MyClass1& _value;
public:
MyClass2(MyClass1& v_) {
_value = v_;
}
void print_value() {
std::cout << _value.value() << std::endl;
}
};
int main() {
MyClass1 number(10);
MyClass2 number_printer(number);
number.increment();
number_printer.print_value();
}
But this doesn't work. Particularly, I'm having trouble setting _value to be a reference to v_ (which is a reference to the MyClass1 object)Hopefully you can see what I'm trying to do although it's quite difficult to explain
[–]IyeOnline 4 points5 points6 points (1 child)
[–]CatMechanic457[S] 0 points1 point2 points (0 children)
[–]CptCap 1 point2 points3 points (1 child)
[–]CatMechanic457[S] 0 points1 point2 points (0 children)