cannot access protected members : why

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

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.
 
J

John Carson

Vincent RICHOMME said:
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.
 
J

Jonathan Mcdougall

Vincent said:
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
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top