[英]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.