Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > nested classes accessing inherited protected members of parents

Reply
Thread Tools

nested classes accessing inherited protected members of parents

 
 
Mr Dyl
Guest
Posts: n/a
 
      12-01-2005
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!!

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-01-2005
Mr Dyl wrote:
> 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;
};
};
 
Reply With Quote
 
 
 
 
Mr Dyl
Guest
Posts: n/a
 
      12-01-2005
It does seem a bit odd that "ptrA->*(&B1::X)" is allowed eh?

Thanks for helping to clear the fog

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing inherited protected Members of superclass durgadeep@gmail.com C++ 1 10-07-2007 05:15 PM
Inherited members of templated classes cpunerd@gmail.com C++ 1 02-07-2007 06:11 AM
Which members are created automatically, which members are not inherited? lovecreatesbeauty C Programming 43 02-06-2006 11:36 PM
Which members are created automatically, which members are not inherited? lovecreatesbeauty C++ 2 01-26-2006 06:03 PM
Accessing members of nested classes jblazi C++ 4 11-08-2003 03:59 PM



Advertisments