友元函数和友元类

在C++中,友元函数和友元类提供了一种机制,可以使非成员函数或其他类访问类的私有成员和保护成员。这种机制在需要对类的内部实现进行特殊访问时非常有用。下面是对友元函数和友元类的详细讲解及示例代码。

友元函数(Friend Function)

友元函数是一个不是类成员的函数,但它却可以访问该类的私有成员和保护成员。友元函数可以是普通函数、成员函数或另一个类的成员函数。

定义和使用友元函数
一个简单的友元函数例子:

#include <iostream>
using namespace std;

class Box {
private:
    double width;
public:
    Box(double w) : width(w) {}

    // 声明友元函数
    friend void printWidth(Box& b);
};

// 定义友元函数
void printWidth(Box& b) {
    cout << "Width of box: " << b.width << endl;
}

int main() {
    Box box(10.0);
    printWidth(box);  // 调用友元函数
    return 0;
}

在上述代码中,printWidth函数被声明为Box类的友元函数,因此它可以访问Box类的私有成员width。

友元类(Friend Class)

友元类是指一个类可以访问另一个类的私有成员和保护成员。通过在类中声明另一个类为友元类,可以实现这一点。

定义和使用友元类
一个简单的友元类例子:

#include <iostream>
using namespace std;

class Square;

class Rectangle {
private:
    double width;
    double height;
public:
    Rectangle(double w, double h) : width(w), height(h) {}

    // 声明友元类
    friend class Square;
};

class Square {
public:
    double getWidth(Rectangle& rect) {
        return rect.width;
    }

    double getHeight(Rectangle& rect) {
        return rect.height;
    }
};

int main() {
    Rectangle rect(10.0, 20.0);
    Square square;
    cout << "Width of rectangle: " << square.getWidth(rect) << endl;
    cout << "Height of rectangle: " << square.getHeight(rect) << endl;
    return 0;
}

在上述代码中,Square类被声明为Rectangle类的友元类,因此Square类中的成员函数可以访问Rectangle类的私有成员width和height。

总结
友元函数:一个不是类成员的函数,但它可以访问该类的私有成员和保护成员。通过在类中使用friend关键字声明友元函数来实现。
友元类:一个类可以访问另一个类的私有成员和保护成员。通过在类中使用friend class关键字声明友元类来实现。
友元函数的更多示例
多个友元函数:

#include <iostream>
using namespace std;

class Point {
private:
    int x, y;
public:
    Point(int x, int y) : x(x), y(y) {}

    // 声明多个友元函数
    friend void setX(Point& p, int x);
    friend void setY(Point& p, int y);
    friend int getX(const Point& p);
    friend int getY(const Point& p);
};

void setX(Point& p, int x) {
    p.x = x;
}

void setY(Point& p, int y) {
    p.y = y;
}

int getX(const Point& p) {
    return p.x;
}

int getY(const Point& p) {
    return p.y;
}

int main() {
    Point p(0, 0);
    setX(p, 10);
    setY(p, 20);
    cout << "Point coordinates: (" << getX(p) << ", " << getY(p) << ")" << endl;
    return 0;
}

友元类的更多示例
多个友元类:

#include <iostream>
using namespace std;

class Alpha;

class Beta {
public:
    void showAlpha(Alpha& a);
};

class Alpha {
private:
    int value;
public:
    Alpha(int v) : value(v) {}

    // 声明友元类
    friend class Beta;
};

void Beta::showAlpha(Alpha& a) {
    cout << "Alpha's value: " << a.value << endl;
}

class Gamma {
public:
    void showAlpha(Alpha& a);
};

void Gamma::showAlpha(Alpha& a) {
    // cout << "Alpha's value: " << a.value << endl; // 这行代码会报错,因为Gamma不是Alpha的友元类
}

int main() {
    Alpha a(123);
    Beta b;
    Gamma g;
    b.showAlpha(a); // 允许访问
    // g.showAlpha(a); // 不允许访问,会报错
    return 0;
}

在上述代码中,Beta类是Alpha类的友元类,因此Beta类可以访问Alpha类的私有成员value。而Gamma类不是Alpha类的友元类,因此不能访问Alpha类的私有成员。

通过以上示例,您可以更好地理解友元函数和友元类的概念及其用法。友元机制在需要访问类的内部实现细节时非常有用,但应谨慎使用,以避免破坏类的封装性。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值