baidut
11/9/2014 - 7:12 AM

友元

友元

#include <iostream>
using namespace std;
#include <string.h>

class Dog{
public:
Dog(char* name){
	strcpy(this->name,name);
}
friend void getName(){
	cout<<"Hi,friend!";
} // 放在public里面
friend void fetchName(Dog* d){
	cout<<"Hi,friend!"<< d->name;
} // 放在public里面
private:
	char name[10];
};

int main() {
	// your code goes here
	Dog d("Tom");
	//getName(); //  error: ‘getName’ was not declared in this scope
	fetchName(&d);
	return 0;
}