1

If I have a box, and inside I have a thousand items in it, I'm going to make a class called Box. Then I have a method in Box which is going to create a thousand items.

class Box
{
public:    
    bool isOpen = true;
    Item *p_boxContents  //Create pointer to a thousand Item objects

    void createBoxContents()
    {
        p_boxContents = new Item[1000];
    }

Now I have an Item class. Remember, I'm going to be creating many of these, and I want each of them to have access to the Box member variable isOpen. How can I do this without giving a pointer member to each instantiation of 'Item' pointing to the Box address?

At first I approached this by using class inheritance, but I was told that was wrong, and I understand that this situation falls more appropriately under the area of composition as opposed to inheritance.

I'm only beginning at programming and I've been trying to figure this out for days, anyone want to give me the obvious answer?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • You should consider using a `std::vector` for the items instead of an array. That will make life a lot easier. – Simon Mar 31 '15 at 12:30
  • why do you always put 2 spaces after period? It's not going like that – phuclv Oct 04 '15 at 05:39

1 Answers1

1

If no code should ever access the Items from outside then you could use and inner class inside Box.

You can read about how the Items would access the members in Box here:

Community
  • 1
  • 1
Simon
  • 6,293
  • 2
  • 28
  • 34
  • In my case the Box class would need access to the Item class as well, it's a sort of mutual dependency which I read was fine to do, say using pointers. I'd want the Box to call the 'update()' method of each of the Item objects, but in keeping to this example, let's say each Item object has a boolean variable 'isHeavy' that the Box class needs to have access to. Anyway, I'm off to read your link. I'd appreciate any help on this. – Zebrafish Mar 31 '15 at 09:16
  • I've looked through your link and another example, but it looks like that solution is storing a reference to the parent class in the child class. So then if you create a thousand of them they're each going to have a reference to the parent class. The reason I ask this is because the parent class is instantiated once, and let's say your were going to instantiate many many of the child class, that would be a pointer, 4 bytes, say times thousands and thousands, but they're all pointing to the same thing. There's no way for them all to point to the same parent class with 1 reference or pointer? – Zebrafish Mar 31 '15 at 09:59
  • I'm still looking for a way, there doesn't seem to be one. The easiest way seems to be for each Item object to have a pointer to the parent class, and to pass the pointer pointing to the Box to each 'Item' as it's created, through maybe a loop. There must be something I'm missing in my head, because I'm stubbornly adamant that you don't need 10 thousand pointers pointing to the same thing, you should should just be able to specify an address in the the child's (update) method. Although, maybe that's the same as having lots of pointers, I'm seriously confused. – Zebrafish Mar 31 '15 at 11:28
  • Passing the reference in through the update method should be fine. You could even just pass `isOpen` in from the update method – Simon Mar 31 '15 at 12:28
  • Thanks, for your help. For now I'm going to pass a pointer to each child object as it's created, but I won't mark this question as answered because my question was to access the parent member without creating a pointer or reference member for each child class created. I don't even know if this is possible or if it makes sense, but it seems a waste, I'm most probably wrong. Now I just have to find a way to instantiate an array of "new" objects passing arguments to each constructor. I've seen it can be done with the std vector, but I'm going to try and do it with an ordinary array. Thanks. – Zebrafish Mar 31 '15 at 12:36