友元函數的使用能夠分如下幾種:ide
(1)類外的函數爲類的友元函數函數
void add_CLA(ADD & add);it
class ADDclass
{總結
private :di
int a;view
int b;vi
public:co
ADD()editor
{
a=1;
b=2;
}
friend void add_CLA(ADD & add);
};
void add_CLA(ADD & add)
{
return (add.a+add.b);
}
(2)類外其它類的成員函數爲本由原函數
class B;
class A
{
private:
public:
A()
{
}
int add(B& b )
{
return (b.a+b.b);
}
};
class B
{
private:
int a;
int b;
public:
B()
{
a=1;
b=1;
}
friend int B::add(B& b);
};
(3)其它類爲本身的友元類
class B;
class A
{
private:
int a;
int b;
public:
A()
{
a=1;
b=1;
}
friend class B;
};
class B
{
private:
public:
int add(A& m_a)
{
return (m_a.a+m_a.b);
}
};
總結完畢!