0

In C++, what happens when I have the following

class House
{
public:
    House();
    ~House();

private:
    int* m_peopleInside;

friend class Room;
}; 

and then in the constructor of House this is set

m_peopleInside = new int[5];
m_peopleInside[4] = 2;

and

class Room
{
public:
    Room();
    ~Room();

    Update();

private:
    int* m_peopleInside;
}; 

Then in the Room.Update() I use m_peopleInside something like this.

&m_peopleInside[4];

It's my understanding that the friend class will allow the Room class to access private members of the House class. So which m_peopleInside would be used?

I should add that in this case, m_peopleInside is being used as an array.

unknownSPY
  • 706
  • 4
  • 15
  • 27

3 Answers3

2

It's an instance variable. So it needs an instance to act on. If no instance is provided, then it is the same as this->m_peopleInside, which means it refers to the instance on which the function was called. So, for example, if this is your function:

void Room::Update() {
    // these two are the same, they null the member of the Room object
    m_peopleInside = nullptr;
    this->m_peopleInside = nullptr;

    House h;
    // should be pretty obvious what this does
    h.m_peopleInside = nullptr;
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Hi, Thanks for the reply. I understand your answer but does it change depending on how it is used? I have added more to my example in the question. – unknownSPY Apr 14 '16 at 15:12
  • @unknownSPY: It changes if you place a different object to the left of it, with a member access operator(`.`). It's not clear at all to me how you think it could possibly access the House member variable (without naming a `House` object). *Which* House object's member would you expect it to access? – Benjamin Lindley Apr 14 '16 at 15:15
  • Very good question. Can I PM you? I have a bit of code to write but doesn't fully relate to the question and its too hard to put it in a comment. – unknownSPY Apr 14 '16 at 15:19
  • @unknownSPY: No. Ask a new question, in a new post. – Benjamin Lindley Apr 14 '16 at 15:19
1

It's my understanding that the friend class will allow the Room class to access private members of the House class.

That is correct.

So which m_peopleInside would be used?

To access the m_peopleInside member of a House object, you will need an object or pointer of type House.

In Room::update(), if you simply use m_peopleInside, it will be member variable of Room, not House.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Hi, Thanks for the reply. I understand your answer but does it change depending on how it is used? I have added more to my example in the question. – unknownSPY Apr 14 '16 at 15:12
  • Yes. Without a `House` object, any use of `m_peopleInside` inside `Room::update` will be the member variable of `Room`, not `House`. – R Sahu Apr 14 '16 at 15:14
0

When you use "m_peopleInside" inside "Room.Update()" you will definitely use the data member of "Room". Your understanding of "friend" classes is not so correct. To make it clear, suppose that you have an object "x" from the class "House" in one of the methods of the class "Room', like "Update()" for example. Then, the following code is correct in this method:

cout << x.m_peopleInside;

Although "m_peopleInside" is private in "House", it is accessible from Room's methods, because the class "House" declares that "Room" is a friend of his.

AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35