Loading class data from one class into another

Say I have class A and class B. A needs to load class data from B into its own variables.

I know I could create a function in A that just calls each of B's getters, but that would be breaking cohesion, no? Is it possible to create a function in B to do this?

This is sort of what I'm looking to do in A:

int x = 0;
int y = 0;

1
2
B exampleObject(x, y); //instance of B
exampleObject.loadData(); //loads values of B's x and y into A's x and y 
OP wrote:
A needs to load class data from B into its own variables.

Are you sure that's the right solution here? I'm just throwing this out there, but could this be a situation where 'A' should inherit from 'B' so that a base pointer to 'A' would be treated as a pointer to an instance of 'B'?

Otherwise make 'A' a friend of 'B' and be done with it. That also breaks cohesion, but it does it with less code :D .
Topic archived. No new replies allowed.