Greetings all,

I'm working on a classic Elevator Simulator problem and have a quick question:

How can I access a private data member of the container class from one of the contained classes?

My Building class (container) "has a" vector of Floor objects and "has a" vector of Elevator objects. How can I have the elevators be able to access the building's floors?

My Instructor has recommended that we stay away from declaring friends if possible.

Thanks!!!

Dave

Thanks for the quick reply!

So I need to set the vector of Floors to protected and then derrive the elevators from class Building? That sounds like its kills the OO part of the program. Sounds like I'm missing something. All I need is for the two "Contained" classes to be able to access eachother.

Elevators shouldn't be a child of Building, they aren't buildings. You could make it public instead of going about all the hackery or adding getFloors/setFloors methods.

Ever hear of a friend class? But friends aren't allowed!

SO, create an access function to READ ONLY the value.


Within the base class.

class Foo
{
private:
    int nFruitCnt;

public:
     Foo();
    virtural ~Foo();

    int GetFruitCount(void) { return nFruitCnt; }
};

So any class that inherits this at any level of inheritance can get the value in the base class.
You can add a set function as well. It's an access member function!

This article has been dead for over six months. Start a new discussion instead.