Moving Data Around

Suppose you have three object.
Object_1
Object_2
Object_3

Object_1 contains Object_2.
Object_2 contains Object_3.
How would I get data, that Object_3 collected, into Object_1?
When you say 'contains', you do mean contains and not 'inherits from'?

If so, an example might be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Object3
{
  int obj3Data;
};

class Object2
{
  Object3 obj3;
};

class Object1
{
  Object2 obj2;
  int dataFromOtherObj;
};

Object1 anObj;

anObj.dataFromOtherObj = anObj.obj2.obj3.obj3Data;


Obviously this is an unrealistic class design and is from an external point-of-view (you'd want to put the assignment code in class member functions).

Cheers,
Jim
Now suppose that Object_3_Data is private. Lol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Object3
{
public:
  int Data() const { return obj3Data; }
private:
  int obj3Data;
};

class Object2
{
  Object3 obj3;
};

class Object1
{
  Object2 obj2;
  int dataFromOtherObj;
};

Object1 anObj;

anObj.dataFromOtherObj = anObj.obj2.obj3.Data();


In this case Object3::Data() is an access function, or a 'getter'.

Edit: Note that the above won't compile as such, as the default permission is private, so all the other members are private, too. You'd need to add a public: to each.
Jim
Last edited on
Oh yes!! :D Thank you very much. :)
Topic archived. No new replies allowed.