karenc@psu-cs.UUCP (Karen Cunningham) (02/09/89)
I am teaching an object-oriented programming class in which I'm including a section on c++. I wanted to show several different ways of allowing (selected parts of) a derived class access to private data of the base class. I succeeded in making several examples work, but had trouble with one. I tried to make a member function of the derived class a friend of the base class. This seems to be the same as the example from page 195 in Stroustrup. I tried to compile that example (it is included below) on several different c++ compilers without success. The message I get occurs at the last line of class employee and states that class manager is undefined. Is this a bug or am I missing something? Karen Cunningham Usenet: {ucbvax,ihnp4,uunet,hplabs}!tektronix!psu-cs!karenc CSNET: karen@cs.pdx.edu InterNet: karen%cs.pdx.edu@relay.cs.net USMail: Karen Cunningham; Computer Science Department; Portland State University; P.O. Box 751; Portland, OR 97207 Phone: (503) 464-4039 ------------------------------Example Follows--------------------------- #include <stream.h> class manager; // This is the example from Stroustrup, page 195. // The second example works (making class manager a friend), // however the first example does not work (making function // "void manager::print" a friend). // // I've tried defining a forward reference to manager::print, // and then making it a friend to employee that didn't work either. class employee { friend void manager::print(); // making this "friend class manager;" works char* name; // ... public: employee* next; employee(char* s) {name = s;} void print() {cout << " employee name is "<< name << "\n";} // ... }; class manager : public employee { // ... public: manager(char* s) : (s) {;} void print(); // ... }; void manager::print() { cout << " manager name is " << name << "\n"; // ... } main() { manager mgr("Hugh"); mgr.print(); }
mwg@inxsvcs.UUCP (Phil Blecker) (02/12/89)
In article <1623@psu-cs.UUCP>, karenc@psu-cs.UUCP (Karen Cunningham) writes: > I tried to make a member function of the > derived class a friend of the base class. This seems to be the same > as the example from page 195 in Stroustrup. I tried to compile that > example ... > without success. I've tried many times to find a way to do that. None of the ports from AT&T's cfront 1.2.1 seem to handle that. Sure wish they would. I'm tired of making classes friends of each other and leaving myself notes to fix it when the compiler accepts the syntax. Oh, well. -- Phil Blecker +1 818 243-3053 none of my ideas belong to me and uunet!inxsvcs!mwg i can't see anything wrong with that