fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class B;
  5.  
  6. class A
  7. {
  8. friend class B;
  9. public:
  10. void publicMethod();
  11. private:
  12. void privateMethod();
  13. };
  14.  
  15. class B
  16. {
  17. friend void A::publicMethod();
  18. friend void A::privateMethod();
  19. private:
  20. void foo();
  21. };
  22.  
  23. void A::publicMethod()
  24. {
  25. B b;
  26. cout << "public ";
  27. b.foo();
  28. privateMethod();
  29. }
  30.  
  31. void A::privateMethod()
  32. {
  33. B b;
  34. cout << "private ";
  35. b.foo();
  36. }
  37.  
  38. void B::foo()
  39. {
  40. cout << "foo\n";
  41. }
  42. int main()
  43. {
  44. A a;
  45. a.publicMethod();
  46. return 0;
  47. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
public foo
private foo