"protected" confusion

R

Rob Richardson

Greetings!

Consider the following:

class CBase
{
public:
CBase();
virtual ~CBase();

protected:
int m_protected;
};

class CDerived : public CBase
{
public:
void TestBase(const CBase& other);
void TestDerived(const CDerived& other);
};

CBase::CBase()
{
m_protected = 1;
}

CBase::~CBase()
{

}

void CDerived::TestBase(const CBase& other)
{
int fromMe = m_protected;
int fromTheOther = other.m_protected; // Error: Can't access
protected member
//
declared in CBase
}

void CDerived::TestDerived(const CDerived& other)
{
int fromMe = m_protected;
int fromTheOther = other.m_protected; // No error
}

When this is compiled in MS Visual C++, the indicated error is thrown
in TestBase(), but TestDerived() does not have a problem. I would
have thought that either I'd get an error in both places or I would
not have gotten any areas. Why am I only getting one error?

Thank you very much.

Rob Richardson
 
V

Victor Bazarov

Rob said:
Consider the following:

class CBase
{
public:
CBase();
virtual ~CBase();

protected:
int m_protected;
};

class CDerived : public CBase
{
public:
void TestBase(const CBase& other);
void TestDerived(const CDerived& other);
};

CBase::CBase()
{
m_protected = 1;
}

CBase::~CBase()
{

}

void CDerived::TestBase(const CBase& other)
{
int fromMe = m_protected;
int fromTheOther = other.m_protected; // Error: Can't access
protected member

// declared in CBase
}

void CDerived::TestDerived(const CDerived& other)
{
int fromMe = m_protected;
int fromTheOther = other.m_protected; // No error
}

When this is compiled in MS Visual C++, the indicated error is thrown
in TestBase(), but TestDerived() does not have a problem. I would
have thought that either I'd get an error in both places or I would
not have gotten any areas. Why am I only getting one error?

Any class has access to any members of the object of the same class.

No class has access to protected members of another class through
their common base class.

The second rule is not as straightforward and the first, but when you
think about it, what if there is a third class, 'COtherDerived', which
derives from 'CBase'. 'CDerived' and 'COtherDerived' are not directly
related (none of them is a successor of the the other), nor are they
friends. They have no special access rights to each other's members.
But you can pass an object of 'COtherDerived' to 'TestBase', right?
The 'CBase' portion of 'COtherDerived' will be given to the 'CDerived'
instance to work with. To prevent access to 'COtherDerived's members
to which that 'CDerived' cannot have access normally, access to the
protected members through a pointer or a reference to the base class
is also prohibited.

V
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top