In accessible base class for return pointer type

Discussion in 'C++' started by Steven T. Hatton, Mar 11, 2006.

  1. Can somebody explain why making T a friend of either B or C will permit the
    code to compile?

    class A{ protected: A(){} };
    class T;
    class B: protected A {protected: B(){}/*friend class T;*/};
    class C: public B {public : C(){}/*friend class T;*/};
    struct T { A* newA() {return new C();}};
    int main() {}

    This is the error I get if I do not use one of the friend declarations:
    In member function ?A* T::newA()?: error: ?A? is an inaccessible base of ?C?

    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
    Steven T. Hatton, Mar 11, 2006
    #1
    1. Advertising

  2. Steven T. Hatton wrote:
    > Can somebody explain why making T a friend of either B or C will
    > permit the code to compile?
    >
    > class A{ protected: A(){} };
    > class T;
    > class B: protected A {protected: B(){}/*friend class T;*/};
    > class C: public B {public : C(){}/*friend class T;*/};
    > struct T { A* newA() {return new C();}};
    > int main() {}
    >
    > This is the error I get if I do not use one of the friend
    > declarations:
    > In member function ?A* T::newA()?: error: ?A? is an inaccessible base
    > of ?C?


    Conversions from a pointer to derived to a pointer base require the
    base to be accessible. If 'A' is a protected base of 'C' (through
    'B'), then no outside class/function can convert a pointer to 'C' to
    a pointer to 'A'. Since 'B' is a public base of 'C', anyone can
    convert a 'C*' to a 'B*'.

    What book on C++ are you reading that doesn't explain access specifiers
    and their effect?


    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 11, 2006
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    > Steven T. Hatton wrote:
    >> Can somebody explain why making T a friend of either B or C will
    >> permit the code to compile?
    >>
    >> class A{ protected: A(){} };
    >> class T;
    >> class B: protected A {protected: B(){}/*friend class T;*/};
    >> class C: public B {public : C(){}/*friend class T;*/};
    >> struct T { A* newA() {return new C();}};
    >> int main() {}
    >>
    >> This is the error I get if I do not use one of the friend
    >> declarations:
    >> In member function ?A* T::newA()?: error: ?A? is an inaccessible base
    >> of ?C?

    >
    > Conversions from a pointer to derived to a pointer base require the
    > base to be accessible. If 'A' is a protected base of 'C' (through
    > 'B'), then no outside class/function can convert a pointer to 'C' to
    > a pointer to 'A'. Since 'B' is a public base of 'C', anyone can
    > convert a 'C*' to a 'B*'.
    >
    > What book on C++ are you reading that doesn't explain access specifiers
    > and their effect?
    >
    >
    > V


    I guess I could/should have looked it up. I just wanted to see what others
    had to say about it. After your suggestion that I access a reference, I
    did take a look at TC++PL(SE) ยง15.3.2. Basically what it says, and what
    you said are the supposition I drew from the example. Part of what was
    unclear in my mind is what scope the conversion takes place in. The
    original code was calling the counterpart of C::newA (above) from within
    the counterpart of A. It took some time to untangle what was happening
    because I had never encountered that particular situation, that I can
    remember.
    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
    Steven T. Hatton, Mar 11, 2006
    #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. tirath
    Replies:
    3
    Views:
    706
    Ivan Vecerina
    Oct 12, 2003
  2. Alberto Luaces
    Replies:
    6
    Views:
    385
    Fraser Ross
    Jun 19, 2006
  3. SzH
    Replies:
    1
    Views:
    350
  4. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
  5. , India
    Replies:
    8
    Views:
    953
    gwowen
    Aug 18, 2010
Loading...

Share This Page