So I've created a class that accepts another object as a reference. The problem is.. I would like to access some of the attributes from the object that is passing the object inside the objects being passed. I really just need two way communication between my objects and I'm not exactly sure of the best way to get this done.
class A {
private:
void someMethod();
public:
A();
};
class B {
protected:
char someAttribute;
A& a;
public:
B(A& a);
};
someMethod() {
char newAttribute = someAttribute;
}
I would like someMethod to have access to someAttribute. I'm not sure if this is even the right way to got about this problem. I'm passing the object into another object in the main function. I'm not sure how much would change if I instanced the class directly inside the the other class.
I also tried making A a child class but it didn't work and I think this would create two separate instances of the parent class which doesn't make any sense to me.
Then I tried using friend.
class A {
private:
void someMethod();
public:
A();
};
class B {
protected:
char someAttribute;
friend class A;
A& a;
public:
B(A& a);
};
someMethod() {
char newAttribute = someAttribute;
}
I'm not sure the right way to do it but the above code doesn't work.
If someone has a much better way of implementing this code, I'm all ears.