当两个类相互需要访问量各类的私有数据时,将函数作为两个类的友元。假设有一个Probe类和一个Analyzer类, 前者表示某种可编程的测量设备,后者表示某种可编程的分析设备:
class Analyzer;
class Probe
{
friend void sync(Analyzer &a, const Probe &p); // sync a to p
friend void sync(Probe &p, cosnt Analyzer &a); // sync p to a
}
class Analyzer
{
friend void sync(Analyzer &a, const Probe &p); // sync a to p
friend void sync(Probe &p, cosnt Analyzer &a); // sync p to a
}
inline void sync(Analyzer &a, const Probe &p)
{
...
}
inline void sync(Probe &p, cosnt Analyzer &a)
{
}