类名

简单,用类名实例化一个对象,即可作为参数进行传递!
定义一个类
class A;
实例化类对象
A a;

定义 函数
int f(A x){};

函数调用
void main()
{
f(a);//a为参数
}
一个函数(包括普通函数和成员函数)可以被多个类声明为“朋友”,可以引用多个类中的私有数据. 
一个函数相当于一个接口,跟所定义的类无关,类中中只是使用它而已,如果不是main函数中定义的函数所有地方都可以使用,给它定义为友元只不过是为了这个函数能使用当前类的私有成员

//=====================================
// EX0807.cpp
// 友元改成普通函数
//=====================================
class Animal{
int itsWeight;
int itsAge;
public:
void setWeight(int w){ itsWeight=w; }
void setAge(int a){ itsAge=a; }
};//-----------------------------------
void setValue(Animal& ta, int tw, int tn){
ta.setWeight(tw);
ta.setAge(tn);
}//------------------------------------
int main(){
Animal peppy;
setValue(peppy, 7, 9);
}//====================================

//----------------------

//=====================================
// EX0807ff.cpp
// 友元改成成员函数
//=====================================
class Animal{
int itsWeight;
int itsAge;
public:
void setValue(int w, int a);
};//-----------------------------------
void Animal::setValue(int tw, int tn){
itsWeight = tw;
itsAge = tn;
}//------------------------------------
int main(){
Animal peppy;
peppy.setValue(7, 9);
}//====================================

 
posted @ 2013-05-16 13:34  herizai  阅读(230)  评论(0编辑  收藏  举报