Two classes in a larger class share data

Go To StackoverFlow.com

1

I have a class that contains objects of two other classes. I need one of the classes to be able to get data from the other one. Here's an example.

class Nom{ /*says what we're eating*/ };
class Chew{ /*stuff that needs to know about what we are eating from nom*/ };

class BigBurrito 
{
   Nom n;
   Chew c;
};
2012-04-03 20:30
by Oatskits
What have you tried?Oliver Charlesworth 2012-04-03 20:31
>
  • finally realizes the value of pointers* :O
  • - Oatskits 2012-04-03 20:45
    thanks everyone, does this have a certain name so that I can learn about it. I'm not too experienced so I'd like to check it out a bit more in depth - Oatskits 2012-04-03 20:46


    1

    How about passing a pointer to the instance of Nom into Chew? Along these lines:

    class Nom {};
    
    class Chew
    {
    private:
        Nom *m_nom;
    public:
        Chew(Nom *nom)
        : m_nom(nom)
        {}
    };
    
    class BigBurrito
    {
    private:
        Nom m_nom;
        Chew m_chew;
    public:
        BigBurrito()
        : m_chew(&m_nom)
        {}
    };
    
    2012-04-03 20:32
    by Stuart Golodetz


    1

    You can either make a pointer to the other class a member of the class

    class Nom{
       Chew* chew;
    };
    class Chew{ /*stuff that needs to know about what we are eating from nom*/ };
    
    class BigBurrito 
    {
       Nom n;  //contains pointer to c
       Chew c;
    };
    

    or pass it via a parameter to the function that performs the operation.

    class Nom
    {
       void performOperationOnChew(Chew& c);
    };
    class Chew{ /*stuff that needs to know about what we are eating from nom*/ };
    
    class BigBurrito 
    {
       Nom n;
       Chew c;
       void doTheOperation()
       {
          n.performOperationOnChew(c);
       }
    };
    

    The second option is cleaner OOP, since Chew doesn't logically belong to Nom.

    2012-04-03 20:33
    by Luchian Grigore
    +1 The whole design in the question seems a little suspect though - if Chew is an action, as I guess in retrospect its name suggests (I find it interesting that my reflex reaction to this without thinking about design issues is pass in a pointer...hmm), then why is it contained in BigBurrito like that? Seems a bit odd - Stuart Golodetz 2012-04-03 20:39
    @StuartGolodetz I actually tried to abstract the names out. I just looked at it as class A, B and C, with composition. : - Luchian Grigore 2012-04-03 20:44
    @StuarGolodetz yeah I just came up with the names as a representation. I didn't really think about them very hard - Oatskits 2012-04-03 20:59


    0

    Just pass in a reference to n (Nom) to your Chew constructor.

    2012-04-03 20:31
    by spencercw
    Well, technically, just adding a parameter to the constructor won't do anything.. - Luchian Grigore 2012-04-03 20:52
    Well the idea was he would do something with it. : - spencercw 2012-04-03 21:24
    Ads