nested classes accessing inherited protected members of parents

Discussion in 'C++' started by Mr Dyl, Dec 1, 2005.

  1. Mr Dyl

    Mr Dyl Guest

    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!!
     
    Mr Dyl, Dec 1, 2005
    #1
    1. Advertising

  2. 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;
    };
    };
     
    Gianni Mariani, Dec 1, 2005
    #2
    1. Advertising

  3. Mr Dyl

    Mr Dyl Guest

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

    Thanks for helping to clear the fog ;)
     
    Mr Dyl, Dec 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jblazi
    Replies:
    4
    Views:
    317
    Chris \( Val \)
    Nov 8, 2003
  2. =?Utf-8?B?cGlyaG8=?=

    web pages, instantiated classes, and parents

    =?Utf-8?B?cGlyaG8=?=, Nov 17, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    330
    =?Utf-8?B?cGlyaG8=?=
    Nov 20, 2006
  3. lovecreatesbeauty
    Replies:
    43
    Views:
    1,323
    Keith Thompson
    Feb 6, 2006
  4. lovecreatesbeauty
    Replies:
    2
    Views:
    590
  5. Replies:
    1
    Views:
    1,286
    Alf P. Steinbach
    Oct 7, 2007
Loading...

Share This Page