T
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?
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?