473,398 Members | 2,393 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,398 software developers and data experts.

discussion of protected in Lippman

t
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members
seems to be wrong, unless I am misinterpreting things. He says:

"A derived class object may access the protected members of its base
class only through a derived object. The derived class has no special
access to the protected members of base type objects."

He gives the following example:

================================================== =======
class Item_base
{
public:
// stuff omitted
protected:
double price;
private:
std::string isbn;
};

class Bulk_item : public Item_base {
// stuff omitted
};

void Bulk_item::memfcn(const Bulk_item&d, const Item_Base& b)
{
// attempt to use protected member
double ret = price; // ok: uses this->price
ret = d.price; // ok: uses price from a Bulk_item object
ret = b.price; // error: no access to price from an Item_base
}

================================================== =======

Am I right?

Sep 21 '07 #1
2 1916
t
Nevermind. I just tried it (which I should have done before
posting). Lippman is correct.

I was confusing:

(1) how a derived class inherits all the members of the base class and
can use the public and protected members, yet cannot use the inherited
private members; in particular, an inherited protected member becomes
a member of the derived class itself;

and

(2) if a derived class object has access to a base class object (this
doesn't include the base class PART of a derived class object), then
it can only access the base class object's public members and not its
protected or private members.

===

Is this correct? This was a bit too subtle for me to catch at first.

Sep 21 '07 #2
On Sep 21, 12:07 pm, t <tmt...@Yahoo.comwrote:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members
seems to be wrong, unless I am misinterpreting things. He says:

"A derived class object may access the protected members of its base
class only through a derived object. The derived class has no special
access to the protected members of base type objects."

He gives the following example:

================================================== =======
class Item_base
{
public:
// stuff omitted
protected:
double price;
private:
std::string isbn;

};

class Bulk_item : public Item_base {
// stuff omitted

};

void Bulk_item::memfcn(const Bulk_item&d, const Item_Base& b)
{
// attempt to use protected member
double ret = price; // ok: uses this->price
ret = d.price; // ok: uses price from a Bulk_item object
ret = b.price; // error: no access to price from an Item_base

}

================================================== =======

Am I right?
No. The description and the example are both correct.
The point to note is this : "a member function can access all (even
private) members of its arguments of the same type"
In your example:

void Bulk_item::memfcn(const Bulk_item&d, const Item_base& b)
{

double ret = price; // uses this->price

ret = d.price; // The argument d is of type Bulk_item which is
same as type of *this. Hence, all members of d are accessible to every
member function of Bulk_item. Hence d.price works.
(Recollect first part of the sentence from book : A derived class
object may access the protected members of its base class only through
a derived object. Thats other part of reason why d.price works. Note
that d.price would not work if you try to access it from a general
global function like "main")

ret = b.price; // (Recollect second part of sentence from the
book : The derived class has no special access to the protected
members of base type objects. Thats why member 'price' of object b,
which is protected, cannot be accessed by general code.)
}
-Neelesh
Sep 21 '07 #3

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

Similar topics

12
by: Steven T. Hatton | last post by:
This is the kind of question that is hard to answer for everybody. Different people think differently. I often stumble where others don't. And I often stroll through what others find hard. I am...
0
by: Harry Smith | last post by:
This was posted in news.groups, but it was not posted to the list. REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.postgresql.admin unmoderated group...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
by: _DS | last post by:
After waiting for Stan Lippman's book on C++/CLI forever, (http://www.amazon.com/gp/product/0321174054/qid=1137406793) it seems to have dropped off the planet. Was publication canceled? I know...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.