2012年(158)
分类: C/C++
2012-11-23 16:31:00
问题的发现:
不能通过编译的代码一:
不能通过编译的代码二:
猜想:对于派生类,protected的受作用者是对象而不是类
用代码来证明:
网友评论2012-11-23 16:33:40
YoYo
B::i is an example of qualified-id.
The reasons of int B::* pmi_B = &B::i; // ill-formed is:
1) First this is an rule: except when forming a pointer to member, the protected access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class). But the B::i is a pointer to member, you must obey it.
2) B::i = 4; // OK (access through this, qualification ignored)
网友评论2012-11-23 16:33:17
YoYo
Sorry, I write an error word "private" => "protected"
Except when forming a pointer to member(definatin bellow), the protected access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class).
A pointer to member is only formed when an explicit "&" is used and its operand is a qualified-id not enclosed in parentheses.
网友评论2012-11-23 16:33:08
YoYo
example from Standard:
class B {
protected:
int i;
static int j;
};
class D1 : public B {
};
class D2 : public B {
friend void fr(B*,D1*,D2*);
void mem(B*,D1*);
};
void D2::mem(B* pb, D1* p1)
{
pb->i = 1; // ill-formed
p1->i = 2; // ill-formed
i = 3; // OK (access through this)
B::i = 4; // OK (access through this, qualification ignore