0

我遇到了这个悬而未决的问题,这真的很令人费解。这种行为应该有意义吗?它在标准中吗?这是问题:

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 
  }
};
4

1 回答 1

1

根据我阅读您发布的链接的理解(另请参见相同的问题here with some answers),原始问题的作者对gcc编译器的行为感到惊讶(2009年的线程,线程中提到的gcc版本4.0.1 ) 编译时没有错误这一行,而它不应该有:

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

我用 MSVC 2010 试过你的代码,这条线没有编译,友谊没有像标准所期望的那样被继承。

于 2012-12-18T12:51:17.453 回答