cannot access protected members : why

Discussion in 'C++' started by Vincent RICHOMME, May 7, 2006.

  1. Hi,

    I still have problems with some class and their members.
    I have two classes declared as :


    typedef struct tagStructB
    {
    int iSizeB;
    char* szTextB;
    } StructB;

    typedef struct tagStructA
    {
    int iSizeA;
    char* szTextA;

    StructB sStructB;
    } StructA;

    class B
    {
    public:

    friend class A;

    B(structB& refStructB) { m_refStructB = refStructB ;}
    int GetSize() {return m_StructB.iSizeB;}
    char* GetText() {return m_StructB.szTextB;}

    protected:
    structB& m_refStructB;
    };


    class A
    {
    public:
    A();
    int GetSize() {return m_StructA.iSizeA;}
    char* GetText() {return m_StructA.szTextA;}
    void Test() { int iTest = m_B.m_refStructB.iSizeB; } // IT WORKS
    B& GetB() { return m_B; }


    protected:
    structA m_StructA;
    B m_B;
    };


    So if I declare class A as a friend of B I CAN have direct access to
    protected members of B.


    Now if I derive a class from A like this:
    class AProp: public A,
    public IPropertyHost
    {
    public:
    virtual void GetProperties( EPropList& PropList )
    {
    int iTest = m_B.m_refStructB.iSizeB; // FAILS !!!!!!
    }


    IPropertyHost* GetPropPointer() { return this; }
    };


    in my GetProperties method, I always have the error message cannot
    access protected member declared in class B.
     
    Vincent RICHOMME, May 7, 2006
    #1
    1. Advertising

  2. Vincent RICHOMME

    John Carson Guest

    "Vincent RICHOMME" <> wrote in message
    news:445df1fa$0$5059$
    > Hi,
    >
    > I still have problems with some class and their members.
    > I have two classes declared as :
    >
    >
    > typedef struct tagStructB
    > {
    > int iSizeB;
    > char* szTextB;
    > } StructB;
    >
    > typedef struct tagStructA
    > {
    > int iSizeA;
    > char* szTextA;
    >
    > StructB sStructB;
    > } StructA;
    >


    This typedef stuff is redundant in C++ (needed only in C).

    > class B
    > {
    > public:
    >
    > friend class A;
    >
    > B(structB& refStructB) { m_refStructB = refStructB ;}
    > int GetSize() {return m_StructB.iSizeB;}
    > char* GetText() {return m_StructB.szTextB;}
    >
    > protected:
    > structB& m_refStructB;
    > };
    >
    >
    > class A
    > {
    > public:
    > A();
    > int GetSize() {return m_StructA.iSizeA;}
    > char* GetText() {return m_StructA.szTextA;}
    > void Test() { int iTest = m_B.m_refStructB.iSizeB; } // IT WORKS
    > B& GetB() { return m_B; }
    >
    >
    > protected:
    > structA m_StructA;
    > B m_B;
    > };
    >
    >
    > So if I declare class A as a friend of B I CAN have direct access to
    > protected members of B.
    >
    >
    > Now if I derive a class from A like this:
    > class AProp: public A,
    > public IPropertyHost
    > {
    > public:
    > virtual void GetProperties( EPropList& PropList )
    > {
    > int iTest = m_B.m_refStructB.iSizeB; // FAILS !!!!!!
    > }
    >
    >
    > IPropertyHost* GetPropPointer() { return this; }
    > };
    >
    >
    > in my GetProperties method, I always have the error message cannot
    > access protected member declared in class B.


    http://www.parashift.com/c -faq-lite/friends.html#faq-14.4

    In future, please copy and paste code. Don't retype it. Your code as
    supplied has lots of errors, which only serves to confuse things.

    --
    John Carson
     
    John Carson, May 7, 2006
    #2
    1. Advertising

  3. Vincent RICHOMME wrote:
    > So if I declare class A as a friend of B I CAN have direct access to
    > protected members of B.
    >
    > Now if I derive a class from A like this:
    > class AProp: public A,
    > public IPropertyHost
    > {
    > public:
    > virtual void GetProperties( EPropList& PropList )
    > {
    > int iTest = m_B.m_refStructB.iSizeB; // FAILS !!!!!!
    > }
    > };
    >
    >
    > in my GetProperties method, I always have the error message cannot
    > access protected member declared in class B.


    Next time, please post a concise and compilable code example.

    Friendship is neither transitive (if A is a friend of B, B is not
    necessarily a friend of A) nor inherited. In your example, A is a
    friend of B but AProp is not, even if it is derived from A. You can
    make AProp ask A to do an operation requiring friendship instead of
    doing that operation directly in AProp.


    Jonathan
     
    Jonathan Mcdougall, May 7, 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. CoolPint
    Replies:
    8
    Views:
    1,000
    Jeff Schwab
    Dec 14, 2003
  2. fabio de francesco
    Replies:
    3
    Views:
    703
    fabio de francesco
    Jun 23, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,071
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    825
    Noah Roberts
    Jun 9, 2006
  5. Replies:
    8
    Views:
    105
Loading...

Share This Page