0

I have a class Controller which has the class Parent as friend:

class Controller {

   // I can use x;

};

class Parent {
   friend class Controller;
   int x;
};

Is there a way to specify that the whole subclass hierarchy of Parent will have Controller as friend without explicitly specifying it in every single class of the 300 I have?

Universal Electricity
  • 775
  • 1
  • 12
  • 26
NoImaginationGuy
  • 1,795
  • 14
  • 24
  • Please take care to post *valid* code in your example. FTFY. – Cheers and hth. - Alf Jun 08 '16 at 12:29
  • Think of it like this. If I have a friend, does it have to be the friend of my son, dad, etc. as well? But my son, dad, etc. can ask me to interact with my friend on their behalf. – Devolus Jun 08 '16 at 12:36
  • Rolled back edit that **changed the question fundamentally** and invalidated part of an existing answer. Don't do that, please. – Cheers and hth. - Alf Jun 08 '16 at 12:37
  • @Cheersandhth.-Alf Lets not get into a rollback war. A mod flag was automatically raised so you can leave it alone and the mods will decide what to do. – NathanOliver Jun 08 '16 at 12:50
  • I don't get why you want to rollback, I miswrote the code and I put the friend clause in the wrong class. My problem was involving `Controller`as friend of `Parent`, not the vice versa, and so I changed the code – NoImaginationGuy Jun 08 '16 at 12:54
  • It's like pulling away a carpet when someone stands on it. In this case an existing answer referred to your original question. When you change the question you make at least that part of the answer seem meaningless or outright wrong. – Cheers and hth. - Alf Jun 08 '16 at 12:56
  • It's okay, and I'm really sorry for that, as I said. But the code I'm talking about is not that. So what's the point of keeping such code? Just to keep an obviously wrong question and an obviously useless answer to it? – NoImaginationGuy Jun 08 '16 at 12:58
  • @Cheers: that's right but, the question makes no sense with the rolled-back snippet... does the OP really have to post a new question for the same thing ? – shrike Jun 08 '16 at 12:59
  • @shrike: No, he or she could for example add a comment about what he/she meant to post, clearly marked as such. That's common. – Cheers and hth. - Alf Jun 08 '16 at 13:01
  • lol......so....what IS the real question? :) – code_fodder Jun 08 '16 at 13:01
  • The question goes with the same code. But the class with the `friend` clause is the second one. – NoImaginationGuy Jun 08 '16 at 13:03

2 Answers2

1

Re

Is there a way to specify that the whole subclass hierarchy of Parent will have Controller as friend without explicitly specifying it in every single class of the 300 I have?

No, but you can just let Parent provide the relevant functionality to its derived classes.

Note that this answers both your original and your amended (fundamentally changed) question.


Wrt. the original question

enter image description here

The comment

// I can use x;

in class Controller, where x is a member of Parent, indicates that you think the friendship that's specified in the Controller class grants Controller access to private parts of Parent.

C++ friendship is single direction, and it goes the opposite way.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    @osnapitzkindle: I rolled back the edit because it invalidated the last part of this answer. Amendments to a question are OK in general. But not fundamental changes. – Cheers and hth. - Alf Jun 08 '16 at 12:38
  • It is not a fundamental change, indeed. It's kinda obvious that this question wouldn't even have made sense if I really had thought about friendship this way. This is **not** the question I asked. – NoImaginationGuy Jun 08 '16 at 12:56
  • Please take more care with the code you post. That includes syntax errors (you forgot the semicolons), and that it exemplifies what you mean and not something else. That makes it easier for us non-telepaths to grok it. – Cheers and hth. - Alf Jun 08 '16 at 12:59
  • The code is just explanatory and, if I'm not wrong, semicolons are not even needed in newer versions of C++. – NoImaginationGuy Jun 08 '16 at 13:05
  • @osnapitzkindle: You're wrong about the syntax, I'm sorry. It has to do with C compatibility. But still it's ungood for beginners, in particular the problem where a beginner omits that semicolon in a header, and the compiler spews out error messages referring to an including file, a different file... – Cheers and hth. - Alf Jun 08 '16 at 13:07
0

You are better off using protected members to keep them private externally but still accessible to your derived classes, that's what this keyword is really for:

class Controller : public Parent
{
    // Now I can use x;
};

class Parent {
protected:
   int x;
};

Note: I am assuming that you want to use a base class...

code_fodder
  • 15,263
  • 17
  • 90
  • 167