Re: class-object member access in aggregation difficult: please suggest alternatives to aggregation
From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 04/22/04
- Next message: Karl Heinz Buchegger: "Re: Dynamic arrays, memory allocation etc."
- Previous message: Toros Caglar: "Dynamic arrays, memory allocation etc."
- In reply to: Chris K: "class-object member access in aggregation difficult: please suggest alternatives to aggregation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 22 Apr 2004 18:13:40 +0200
Chris K wrote:
>
> Hi,
>
> I have used composition to define a class that contains an array
> of classes.
>
> class Ensemble{
> private:
> ElementClass **elements;
>
> public:
> some methods ...
> }
>
> class ElementClass{
> private:
> some members;
>
> public:
> some methods ...
> }
>
> In the constructor I define an array of elements and everything
> works fine.
>
> However, I have come to realize that accessing private members of
> elements in methods belonging to Ensemble is a pain and I do actually
> need to do that.
>
> It seems that the only way - except for making everything public - would
> be walk through two layers of getSomething() methods.
There is a third approach:
ElementClass could declare Ensemble to be a friend and thus grant
Ensemble the right to access private members.
class ElementClass {
friend class Ensemble;
private:
some members;
public:
some members;
};
-- Karl Heinz Buchegger kbuchegg@gascad.at
- Next message: Karl Heinz Buchegger: "Re: Dynamic arrays, memory allocation etc."
- Previous message: Toros Caglar: "Dynamic arrays, memory allocation etc."
- In reply to: Chris K: "class-object member access in aggregation difficult: please suggest alternatives to aggregation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|