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


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


Relevant Pages

  • Re: Magazine article
    ... websites. ... intended for private circulation among members of the ... and supplying information privately to family members. ... family members aren't family historians, ...
    (soc.genealogy.britain)
  • Re: Closing parenthesis in functions definition followed by its call
    ... "properties of the constructor"? ... Java's notion of static members, ... This pattern of public, private, and privileged members is possible ... The pattern in question is the one for emulating private instance ...
    (comp.lang.javascript)
  • Re: Method of Unit Testing Private Methods
    ... > into the problem of how to properly unit test private methods of a ... I follow the philosophy that, if it's private, then its none of my ... members, via the correctness of the public members that use those ... Private Sub Foo() ...
    (comp.programming)
  • Re: (Probably) easy question about inheritance
    ... Typically a matrix uses a two-dimensional array, so it would be private ... I would like to access three data members: ... public inheritance is the same thing as inheritance in Java) ...
    (comp.lang.java.programmer)
  • Re: modular programming in Forth
    ... Foo \ Depends on Foo ... To prevent private members from being used outside the module. ... I feel this is a valid reason for the public/private distinction, ...
    (comp.lang.forth)