473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

"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

Aug 1 '07 #1
2 1630
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
Aug 1 '07 #2
Thank you very much, Viktor. That makes sense.

Rob

Aug 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
2
by: Andreas Klemt | last post by:
Hello, what is the difference between a) Protected WithEvents myClassName b) Protected myClassName Thanks, Andreas
3
by: Jordan Taylor | last post by:
I am confused about protected member functions and objects. Is there any particular advantage of declaring members protected?
4
by: Tina | last post by:
This is an issue regarding what exactly is accomplished by using "Protected" when defining a variable. It seems it does much more than just applying Protected status to a variable. I have an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.