0

I came across this unanswered question and it is really puzzling. Is this behavior supposed to make any sense? Is it in the standard? This is the question:

class parentFriend;
class parent
{
  friend class parentFriend;
  public: void f1(){};
  protected: void f2(){};
  private: void f3(){};
};
class childpub: public parent {};
class childprot: protected parent {};
class childprv: private parent {};
class parentFriend
{
  public:
  void f()
  {
    /* which of the following statements will compile? why? why not?*/
    parent p; p.f1(); p.f2(); p.f3(); // these will compile 
    childpub cpub; cpub.f1(); cpub.f2(); cpub.f3();
    childprot cprot; cprot.f1(); cprot.f2();
    cprot.f3(); // does not compile 
    childprv cprv;
    cprv.f1(); // does not compile 
    cprv.f2(); // does not compile 
    cprv.f3(); // does not compile 
  }
};
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Doru Georgescu
  • 309
  • 2
  • 12

1 Answers1

1

From what I understood reading the link you posted (see also the same question here with some answers), the author of the original question was surprised by the behaviour of the gcc compiler (thread from 2009, gcc version 4.0.1 mentionned in the thread) which was compiling without error this line whereas it shouldn't have:

childprot cprot; cprot.f1(); cprot.f2(); // (2)

I tried your code with MSVC 2010 and this line doesn't compile, the friendship is not inherited as expected by the standard.

Étienne
  • 4,773
  • 2
  • 33
  • 58
  • Great, thank you for this. My gcc 4.2.4 still behaves like the one in the original post. Please tell me if anything besides cpub.f1() and of course, p.f?(), compiles, because they should not. – Doru Georgescu Dec 19 '12 at 08:10