C++/Class/Friend
Содержание
Define friend function for
<source lang="cpp">
- include <iostream>
- include <cstring>
- include <cstdlib>
using namespace std; class StringClass {
char *p; int len;
public:
StringClass(char *ptr); ~StringClass() { delete [] p; } friend ostream &operator<<(ostream &stream, StringClass &ob);
}; StringClass::StringClass(char *ptr) {
len = strlen(ptr)+1; p = new char [len]; if(!p) { cout << "Allocation error\n"; exit(1); } strcpy(p, ptr);
} ostream &operator<<(ostream &stream, StringClass &ob) {
stream << ob.p; return stream;
} int main() {
StringClass stringObject1("www.java2s.com"), stringObject2("www.java2s.com"); cout << stringObject1; cout << endl << stringObject2 << endl; return 0;
}
</source>
friend class for each other
<source lang="cpp">
- include <iostream>
- include <string.h>
using namespace std; class Curly {
public: Curly(char *msg) { strcpy(message, msg); }; void show_message(void) { cout << message << endl; }; friend class Moe; void show_moe(class Moe moe); private: char message[256]; };
class Moe {
public: Moe(char *msg) { strcpy(message, msg); }; void show_message(void) { cout << message << endl; }; friend class Curly; void show_curly(class Curly curly); private: char message[256]; };
void Curly::show_moe(class Moe moe) { cout << moe.message << endl; }; void Moe::show_curly(class Curly curly) { cout << curly.message << endl; }; int main(void) {
class Moe moe("nuck..."); class Curly curly("whoop..."); moe.show_message(); moe.show_curly(curly); curly.show_message(); curly.show_moe(moe);
}
</source>
Friend function Demo
<source lang="cpp">
- include <iostream>
using namespace std; class myclass {
int num;
public:
myclass(int x) { num = x; } friend int isneg(myclass ob);
}; int isneg(myclass ob) {
return (ob.num < 0) ? 1 : 0;
} int main() {
myclass a(-1), b(2); cout << isneg(a) << " " << isneg(b); cout << endl; return 0;
}
</source>
Friends can access private members of a class.
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl; class Count {
friend void setX( Count &, int );
public:
Count() { x = 0; } void print() const { cout << x << endl; }
private:
int x;
}; void setX( Count &c, int val ) {
c.x = val; // legal: setX is a friend of Count
} int main() {
Count counter; cout << "counter.x after instantiation: "; counter.print(); cout << "counter.x after call to setX friend function: "; setX( counter, 8 ); // set x with a friend counter.print(); return 0;
}
</source>
Make the inserter into a friend function
<source lang="cpp">
- include <iostream>
- include <cstring>
using namespace std; class PhoneNumber {
char name[80]; int areaCode; int prefix; int num;
public:
PhoneNumber(char *n, int a, int p, int nm) { strcpy(name, n); areaCode = a; prefix = p; num = nm; } friend ostream &operator<<(ostream &stream, PhoneNumber o);
}; // Display name and phone number. ostream &operator<<(ostream &stream, PhoneNumber o) {
stream << o.name << " "; stream << "(" << o.areaCode << ") "; stream << o.prefix << "-" << o.num << "\n"; return stream; // must return stream
} int main() {
PhoneNumber a("T", 111, 555, 1234); PhoneNumber b("A", 312, 555, 5768); PhoneNumber c("T", 212, 555, 9991); cout << a << b << c; return 0;
}
</source>
Use friend function to access the non-public member variable
<source lang="cpp">
- include <iostream>
using namespace std; class MyClass {
int a, b; public: friend int sum(MyClass x); void set_ab(int i, int j);
}; void MyClass::set_ab(int i, int j) {
a = i; b = j;
} int sum(MyClass object) {
return object.a + object.b;
} int main(void) {
MyClass integer; cout << "Adding 3 and 4:" << endl; integer.set_ab(3,4); cout << sum(integer);
}
</source>