"protected" confusion



 DEVELOP > c-Plus-Plus > "protected" confusion

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Rob Richardson"
Date: 01 Aug 2007 08:24:55 AM
Object: "protected" confusion
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
.

User: "Victor Bazarov"

Title: Re: "protected" confusion 01 Aug 2007 09:10:40 AM
Rob Richardson wrote:

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "Rob Richardson"

Title: Re: "protected" confusion 01 Aug 2007 09:50:29 AM
Thank you very much, Viktor. That makes sense.
Rob
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER