nested classes accessing inherited protected members of parents

M

Mr Dyl

There's probably a simple explanation for this and it's just too late
for my brain to work. Does anybody know a good reason why class B2
compiles but class B1 doesn't?

class A {
public:
class Nested_A {
protected:
A* ptrA;
};
protected:
int X;
};

class B1 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrA->X);}
// error: protected member "A::X" is not accessible through a "A"
pointer or object
};
};

class B2 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrB->X);} // Happy compiler

protected:
B* ptrB;
};
};


Thanks!!
 
G

Gianni Mariani

Mr said:
There's probably a simple explanation for this and it's just too late
for my brain to work. Does anybody know a good reason why class B2
compiles but class B1 doesn't? ....

It's because A::X is being accessed through a pointer other than a
pointer that the scope has visibility. i.e. Nested_B does not have
access to protected parts of A, but Nested_A does. B1 and B2 does have
visibility into the protected parts of A.

What's weird (and logically a hole in the standard IMHO), is that you
can take a member pointer to B1::X - which is exactly the same as A::X,
but since B1::Nested_B has visibility into B1, it can take B1::X which
the allows you to directly access A::X from an A&. See below.

Probably the best thing is to provide an accessor in Nested_A that
promotes the visibility to classes derived from Nested_A (see GetAX()
below).

class A {
public:
class Nested_A {
protected:
int & GetAX()
{
return ptrA->X;
}
A* ptrA;
};
protected:
int X;
};

class B1 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrA->X);}
// error: protected member "A::X" is not accessible through a "A"
pointer or object
int foo1() {return(ptrA->*(&B1::X));}
int foo2() {return(GetAX());}
};
};

class B2 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrB->X);} // Happy compiler
int foo1() {A* ptrA=ptrB; return(ptrA->X);} // UNhappy compiler

protected:
B2* ptrB;
};
};
 
M

Mr Dyl

It does seem a bit odd that "ptrA->*(&B1::X)" is allowed eh?

Thanks for helping to clear the fog ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top