NDK开发---C++学习(五):类与函数(下)

前言

上中两篇已经介绍了构造函数、成员函数、析构函数、成员初始化列表、this指针等,本篇将介绍友元机制、运算符重载。

友元函数、友元类和友元成员函数

类的封装性具有信息隐藏的能力,它使外部函数只能通过类的public成员函数(数据成员常被设为私有权限)才能访问类的private成员。如果有函数多次访问类的私有成员,就要多次访问类的公有成员函数,使操作变得很繁琐。能否给某些函数特权,让它们可以直接访问类的私有成员呢?C++给出的答案是友元(friend)。友元机制允许类授予其他函数直接访问类的非公有成员(即privateprotected成员)的特权。

友元函数

#include<iostream>
using namespace std;
class A {
private:
    int i;
public:
    A(int i) {
        this->i = i;
    }

    void myPrintf() {
        cout << this->i << endl;
    }
    //友元函数
    friend void modify_i(A* P, int a);
};

//友元函数的实现,在友元函数中可以访问私有的属性
void modify_i(A* p, int a) {
    p->i = a;
}

void main() {
    A* a = new A(10);
    a->myPrintf();

    modify_i(a, 20);
    a->myPrintf();

    getchar();
}

运行打印结果为:

10
20

类的友元函数是一种特殊的普通函数,它可以直接访问该类的私有成员。关键字friend用于声明友元,它只能出现在类的声明中。
友元函数只是一个普通函数而非类的成员函数,所以它不受publicprotectedprivate的限定,无论将它放在public区,或者protected区,还是private区,效果完全相同。
友元不具逆向性和传递性。即若A是B的友元,并不表示B是A的友元(除非特别声明);若A是B的友元,B是C的友元,也不能代表A是C的友元(除非特别声明)。

友元类

一个类可以是另一个类的友元,友元类的所有成员函数都是另一个类的友元函数,能够直接访问另一个类的所有成员(包括publicprivateprotected)。

#include<iostream>
using namespace std;
class A {
    //友元类
    friend class B;
private:
    int i;
public:
    A(int i) {
        this->i = i;
    }
    void myPrintf() {
        cout << this->i << endl;
    }
};

class B {
public:
    //B这个友元类可以访问A类的任何成员
    void accessAny() {
        a.i = 30;
    }
private:
    A a;
};

友元成员函数

#include<iostream>
using namespace std;
class B;                //L1:向前引用声明
class A {
private:
    int x, y;
public:
    A(int x, int y) {
        this->x = x;
        this->y = y;
    }
    int sum(B b);       //L2:在此只能声明sum(),其定义须在class B定义之后
};

class B {
private:
    int z;
public:
    B(int z = 0) {
        this->z = z;
    }
    friend int A::sum(B b); 
};

int A::sum(B b) {       //L3:sum()的定义只能在B定义之后
    return x + y + b.z;
}

void main() {
    A a(2, 3);
    B b(4);
    cout << a.sum(b) << endl;

    getchar();
}

运行打印结果为:

9

注意:

  1. 语句L1处的向前声明class B是必须的,因为在声明函数A::sum()的参数表中,用了类B作为参数,而此时类B尚未定义。
  2. 一个类的成员函数作为另一个类的友元时,必须先定义这个类。在上例中,A必须先定义,B后定义,若先定义B是不行的。
  3. 友元函数应在两个类都定义了之后再定义。作为友元的成员函数,可以直接访问两个类的私有成员和保护成员,它能把两个类结合起来传递和处理数据,使类高效地工作。

总结:

友元使编程更简洁,程序运行效率更高。但它可以直接访问类的私有成员,破坏了类的封装性和信息隐藏。其实对比Java,友元类是不是特别像Class cls = Class.forName("com.zhangpan.JNICmake")中的cls,同样可以访问私有成员和保护成员,是不是对Java和C++的区别和联系又多了一层了解呢?

运算符重载

运算符重载是C++的一项强大功能。通过重载,可以扩展C++运算符的功能,使它们能够操作用户自定义的数据类型,增加程序代码的直观性和可读性。
运算符可以以普通函数、类的非静态成员函数和类的友元函数三种方式重载。

普通函数运算符重载

#include<iostream>
using namespace std;
class Point {
public:
    int x;
    int y;

    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
    void myPrintf() {
        cout << x << " ," << y << endl;
    }
};

//重载+号
Point operator+(Point &p1, Point &p2) {
    Point temp(p1.x + p2.x, p1.y + p2.y);
    return temp;
}

void main() {
    Point p1(10, 20);
    Point p2(10, 40);
    Point p3 = p1 + p2;
    p3.myPrintf();

    getchar();
}

运行打印结果为:

20, 60

类的非静态成员运算符函数重载

#include<iostream>
using namespace std;
class Point {
private:
    int x;
    int y;
public:
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
    
    Point operator+(Point &p) {
        Point temp(this->x + p.x, this->y + p.y);
        return temp;
    }

    void myPrintf() {
        cout << x << " ," << y << endl;
    }
};

void main() {
    Point p1(10, 20);
    Point p2(10, 40);

    //运算符的重载,本质上还是函数调用
    //就相当于p1.operator+(p2)
    Point p3 = p1 + p2;
    p3.myPrintf();

    getchar();
}

运行打印结果为:

20, 60

类的友元运算符函数重载

using namespace std;
class Point {
    friend Point operator+(Point &p1, Point &p2);
private:
    int x;
    int y;
public:
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }

    void myPrintf() {
        cout << x << "," << y << endl;
    }
};

Point operator+(Point &p1, Point &p2) {
    Point temp(p1.x + p2.x, p1.y + p2.y);
    return temp;
}

void main() {
    Point p1(10, 20);
    Point p2(10, 40);

    //运算符的重载,本质上还是函数调用
    //就相当于p1.operator+(p2)
    Point p3 = p1 + p2;
    p3.myPrintf();

    getchar();
}

运行打印结果为:

20, 60

展望

    原文作者:zhang_pan
    原文地址: https://www.jianshu.com/p/1b5a11ba28de
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞