< />

C++友元类 kisara

C++友元类以及共同的友元

class Tv {
private:
    int state;
    int volume;
    ...
public:
    friend class Remote;//友元类 Tv类决定谁是他的友元
    enum {Off,On};
    ...
};
class Remote{
private:
    int mode;
public:
    Remote(int m=Tv::Tv):mode(m){}
    bool volup(Tv &t){ return t.volup}//友元类可以对朋友的私有成员直接访问
    bool voldown(Tv &t){ return t.volume}
    ...
};
class Probe;//共同的友元
class Analyzer{
public:
    friend void sync(Analyzer& a, const Probe &p);
};
class Probe{
public:
    friend void sync(Analyzer &a, const Probe &p);
};
inline void sync(Analyzer &a, const Probe& p){

}
kisarawechat kisaraalipay