C++中的友元探究

本文探讨了C++中的友元概念,包括友元类和友元成员函数的使用,以及如何处理复杂的友元关系。通过示例解释了如何在类间建立友元关系,以增强代码的灵活性和效率,同时避免暴露不必要的公有接口。

        类并非只能拥有友元函数,也可以将类作为友元。在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员。另外,也可以做更加严格的限制,只将特定的成员函数指定为另一个类的友元。友元提高了公有接口的灵活性。

1. 友元类

        友元类的声明可以位于公有、私有或保护部分,其所在的位置无关紧要。

// 程序清单1 tv.h -- Tv and Remote classes

#ifndef TV_H_
#define TV_H_

class Tv
{
public:
    friend class Remote;         // Remote类可以改动TV类的私有部分
    enum {Off, On};
    enum {MinVal, MaxVal = 20};
    enum {Antenna, Cable};
    enum {TV, DVD};

    Tv(int s = Off, int mc = 125): state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
    void onoff() { state = (state == On) ? Off : On; }
    bool ison() const { return state == On; }
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
    void set_input() { input = (input == TV) ? DVD : TV; }
    void settings() const;  // display all settings

private:
    int state;     // on or off
    int volume;    // assumed to be digitized
    int maxchannel;
    int channel;   // current channel setting
    int mode;      // broadcast or cable
    int input;     // TV or DVD
};

class Remote
{
public:
    Remote(int m = Tv::TV): mode(m) {}
    bool volup(Tv& t) { return t.volup(); }
    bool voldown(Tv& t) { return t.voldown(); }
    void onoff(Tv& t) { t.onoff(); }
    void chanup(Tv& t) { t.chanup(); }
    void chandown(Tv& t) { t.chandown(); }
    void set_chan(Tv& t, int c) { t.channel = c; }
    void set_mode(Tv& t) { t.set_mode(); }
    void set_input(Tv& t) { t.set_input(); }

private:
    int mode;
};

#endif

        注意到,除构造函数外,所有的Remote方法都将一个Tv对象引用作为参数,这标明遥控器必须针对特定的电视机。

        如果两种状态值分别为true(1)和false(0),则可以结合使用按位异或和赋值运算符(^=)来简化状态切换代码:

void onoff() { state ^= 1; }
//程序清单2 tv.cpp -- methods for the Tv class (Remote methods are inline)
#include <iostream>
#include "tv.h"

bool Tv::volup()
{
    if(volume < MaxVal)
    {
        volume++;
        return true;
    }
    else
        return false;
}

bool Tv::voldown()
{
    if(volume > MinVal)
    {
        volume--;
        return true;
    }
    return false;
}

void Tv::chanup()
{
    if(channel < maxchannel)
        channel++;
    else
        channel = 1;        // !!注意这样可以循环一直加
}

void Tv::chandown()
{
    if(channel > 1)
        channel--;
    else
        channel = maxchannel;
}

void Tv::settings() const
{
    using std::cout;
    using std::endl;
    cout << "TV is " << (state == Off ? "Off" : "On") << endl;
    if(state == On)
    {
        cout << "Volume setting = " << volume << endl;
        cout << "Channel setting = " << channel <<  endl;
        cout << "Mode = " << (mode == Antenna ? "antenna" : "cable") << endl;
        cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
    }
}

        程序清单3所示的简单的程序可以测试一些特性,另外,可使用同一个遥控器控制两台不同的电视机。

int main()
{
    using std::cout;
    Tv s42;
    cout << "Initial settings for 42\" TV:\n";
    s42.settings();
    s42.onoff();
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.settings();

    Remote grey;

    grey.set_chan(s42, 10);
    grey.volup(s42);
    grey.voiup(s42);
    cout << "\n42\" settings after using remote:\n";
    s42.settings();

    Tv s58(Tv::On);
    s58.set_mode();
    gery.set_chan(s58, 28);
    cout << "\n58\" settings:\n";
    s58.settings();

    return 0;
};

        记住,类是一种自然用语,用于表示一些关系。如果不使用某些形式的友元关系,则必须将Tv类的私有部分声明为公有的,或创建一个笨拙的大型类来包含电视机和遥控器。这种解决方法无法反应这样的事实,即同一个遥控器可用于多台电视机。

2. 友元成员函数

        上一个例子中,大多数Remote方法都是用Tv类的公有接口实现的。这意味着这些方法不是真正需要友元。实际上唯一需要作为友元的方法是Remote::set_chan(),因此仅需要让特定的类成员成为另一个类的友元,而不必让整个类都成为友元。但这样有点麻烦,需要小心排列各种声明和定义的顺序:

        需要在Tv类中将Remote::set_chan()方法声明为友元。

class Tv
{
    friend void Remote::set_chan(Tv& t, int c);
// ...
};

        这里注意,应使用前向声明。需要在Remote定义的前面插入下面的语句:

class Tv;
class Remote { };
class Tv { };

        注意,在编译器在Tv类的声明中看到Remote的一个方法被声明为Tv类的友元之前,应该先看到Remote类的声明和set_chan()方法的声明。

        另外,由于Remote声明包含了内敛代码,例如:

void onoff(Tv& t) { t.onoff(); }

        由于这将调用Tv的一个方法,所以编译器此时必须已经看到了Tv类的声明,这样才能知道Tv有哪些方法,但正如看到的,该声明位于Remote声明的后面。这种问题的解决办法是,在Remote声明中只包含方法声明,并将实际的定义放在Tv类之后。

//程序清单4 tvfm.h
#ifndef TVFM_H_
#define TVFM_H_

class Tv;
class Remote
{
public:
    enum State{Off, On};
    enum {MinVal, MaxVal = 20};
    enum {Antenna, Cable};
    enum {TV, DVD};

private:
    int mode;

public:
    Remote(int m = TV): mode(m) {}
    bool volup(Tv& t);
    bool voldown(Tv& t);
    void onoff(Tv& t);
    void chanup(Tv& t);
    void chandown(Tv& t);
    void set_mode(Tv& t);
    void set_input(Tv& t);
    void set_chan(Tv& t, int c);
};

class Tv
{
public:
    friend void Remote::set_chan(Tv& t, int c);  //友元成员函数
    enum State{Off, On};
    enum {MinVal, MaxVal = 20};
    enum {Antenna, Cable};
    enum {TV, DVD};

    Tv(int s = Off, int mc = 125): state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
    void onoff() { state = (state == On) ? Off : On; }
    bool ison() const { return state == On; }
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
    void set_input() { input = (input == TV) ? DVD : TV; }
    void settings() const;  // display all settings

private:
    int state;     // on or off
    int volume;    // assumed to be digitized
    int maxchannel;
    int channel;   // current channel setting
    int mode;      // broadcast or cable
    int input;     // TV or DVD
};

// Remote methods as inline functions
inline bool Remote::volup(Tv& t) { return t.volup(); }
inline bool Remote::voldown(Tv& t) { return t.voldown(); }
inline void Remote::onoff(Tv& t) { t.onoff(); }
inline void Remote::chanup(Tv& t) { t.chanup(); }
inline void Remote::chandown(Tv& t) { t.chandown(); }
inline void Remote::set_mode(Tv& t) { t.set_mode(); }
inline void Remote::set_input(Tv& t) { t.set_input(); }
inline void Remote::set_chan(Tv& t, int c) { t.channel = c; }

#endif

        注意内联函数的链接性是内部的,这意味着函数定义必须在使用函数的文件中。在这个例子中,内敛定义位于头文件中,因此在使用函数的文件中包含头文件可确保将定义放在正确的地方。也可以将定义放在实现文件中,但必须删除关键字inline,这样函数的链接性将是外部的。

        By the way, 让整个Remote类成为友元并不需要前向声明,因为友元语句本身已经指出Remote是一个类:friend class Remote;

3. 其他友元关系

3.1 让类彼此成为对方的友元

        记住对于使用Remote对象的Tv方法,其原型可在Remote类声明之前声明,但必须在Remote类声明之后定义,以便编译器有足够的信息来编译该方法。

class Tv
{
    friend class Remote;
public:
    void buzz(Remote& r);
    //...
};
class Remote
{
    friend class Tv;
public:
    void volup(Tv& t) { t.volup(); }
}
inline void Tv::buzz(Remote& r)
{
    
}

4. 共同的友元

        需要使用友元的另一种情况是,函数需要访问两个类的私有数据。从逻辑上来看,这样的函数应该是每个类的成员函数,但这是不可能的。它可以是一个类的成员,同时是另一个类的友元,但有时将函数作为两个类的友元更合理。例如,假定有一个Probe类和一个Analyzer类,前者表示某种可编程的测量设备,后者表示某种可编程的分析设备。这两个类都有内部时钟,且希望它们能够同步,则应该包含下述代码行:

class Analyzer;
class Probe
{
    friend void sync(Analyzer& a, const Probe& p);   // sync a to p
    friend void sync(Probe& p, const 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, const Analyzer& a);   // sync p to a
    // ...
};
//define the friend functions
inline void sync(Analyzer& a, const Probe& p)
{
    // ...
}
inline void sync(Probe& p, const Analyzer& a)
{
    // ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值