thh@hpindda.HP.COM (Tom Harper) (03/07/89)
I have a question concerning the visibility of member names. I cannot get
an example to work as I expect based on the information found on page
281 of Stroustrup's "The C++ Programming Language". Hopefully someone can
point out the error of my ways to me.
The following code works as I expect:
#include <stream.h>
class baseclass {
int a;
public:
baseclass();
int c;
};
baseclass::baseclass() { c = 13; }
class derived : public baseclass {
public:
derived();
};
derived::derived() { ; }
void ef (derived& a) { cout << "a.c: " << a.c << "\n"; }
main() { derived a; ef(a); }
The external function ef has visibility of baseclass member c.
Running the executable produces the output "a.c: 13". No surprises for me
here.
Example two restricts the base class in the derived class definition:
#include <stream.h>
class baseclass {
int a;
public:
baseclass();
int c;
};
baseclass::baseclass() { c = 13; }
class derived : private baseclass {
public:
derived();
baseclass::c; // Hopefully make c visible to derived class objects.
};
derived::derived() { ; }
void ef (derived& a) { cout << "a.c: " << a.c << "\n"; }
main() { derived a; ef(a); }
This results in a compiler error telling me that c is a private member. The
message is: "ef() cannot access c: baseclass is a private base class.". I
expected that c would be visible to objects of class derived, since it is
declared in the public member list. What am I not correctly understanding?
Tom Harper thh@hpindda.HP.COM