简体   繁体   English

基础访问的朋友派生

[英]friend of base accesses derived

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

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: 从我的理解读你发布的链接(也看到了同样的问题, 这里有一些答案),原题的作者是由gcc编译器(从2009年线程的行为感到惊讶,gcc版本4.0.1中的线程mentionned )正在编译而没有错误的这一行,但不应包含:

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. 我在MSVC 2010中尝试了您的代码,但此行无法编译,友谊没有按标准所预期的那样继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基类和派生类的朋友功能 - friend function of base class and derived class 如何在CRTP中使参数化Base成为Derived的朋友? - How to make parameterized Base a friend of Derived in CRTP? 在派生类中重新定义基类的Friend函数 - Redefining Friend Function of Base Class in Derived Class 有没有办法派生基本函数的实现来访问base是朋友的类? - Is there a way for a derived implementation of a base function to access a class where base is a friend? 在基类/派生类的成员函数/好友函数中派生*到Base *的转换 - Derive* to Base* conversion in member function/friend function of base/derived class 基数 class 和该基数 class 的派生 class 能否有一个共同的朋友 class,它是两个类的朋友? - Can a base class and a derived class of that base class have a common friend class which is a friend of both the classes? c++ 派生 class 访问基类的友元运算符 - c++ derived class accessing base class' friend operators c ++派生的基类Friend函数在Parent上访问private? - c++ derived base class Friend function accessing private on Parent? 使用派生类的朋友提供的基类的受保护静态函数 - Use protected static function of the base class from the friend of the derived class 如何将 Variadic CRTP 基类设置为派生 class 的朋友 - How to set Variadic CRTP base classes to be friend's of the derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM