“模板类与友元”那些事(C++)

来源:互联网 发布:网络赌钱正规吗 编辑:程序博客网 时间:2024/06/07 12:50

模版类就是类模版实例化之后的类,友元就是一种对函数访问权限的控制,通过将函数设为友元函数让其能够访问其他外部函数不能访问的"private"成员变量。

       接着我们介绍一个他们结合在一起会产生什么样的结果,他们的结合分为三种情况:

        1、模板类的非模板友元函数

        该友元函数的特点是:

        A)当该友元函数不使用类模版中的成员变量时,与一般的友元函数没有区别:

        B)当该友元函数使用类模版中的成员变量时,必须为每一个基本类型定义一个友元函数,比如show<int> 和 show<double>.这两个并不是函数的重定义,属于函数重载。

   测试代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

template<typename T>

class Base{

public:

    Base(T x, T y): x(x), y(y){}

    friend void print();

    friend void show(Base<T> &a);

private:

    T x;

    T y;

};

void show(Base<int> &a)

{

    cout << "x = " << a.x << ", y = " << a.y << endl;

}

void show(Base<double> &a)

{

     cout << "x = " << a.x << ", y = " << a.y << endl;

}

void print(){

    cout << "hello, world" << endl;

}

int main()

{

    Base<int> ai(99, 999);

    Base<double> ad(99.99, 200.88);

    print();

    show(ai);

    show(ad);

    return 0;

}

        2、模板类的约束友元函数

            该友元函数的特点是:每一个类的具体化与友元的具体化要匹配,也就是说,int类具体化获得一个int类的友元函数,double类就具体化会获得一个double函数,int类具体化不可能获得double类函数;

           要定义一个约束模板友元函数分三步:

        A)在定义类之前声明友元函数模板;

        B)在类中声明该友元函数模板;

        C)定义友元函数,

         注意,定义友元函数时形参列表中的”T“代表,具体化之后的类,并不是类的模板参数,也就是假如具体化一个模板类”A<int>“, ”T“代表”A<int>“, 并不是”int“;通过这种方式来约束该友元函数属于某个具体化之后的模板。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

template<typename T>void print(); // 第一步:在类定义之前生命函数模板

template<typename T>void show(T &t);

  

template<typename T>

class Base{

public:

    Base(T x, T y): x(x), y(y){}

    friend void print<T>();  // 第二步:在类中声明友元函数模板

    friend void show<>(Base<T>& a);

private:

    T x;

    T y;

};

//第三步:定义友元函数

void print()

{

    cout << "hello, friend function template" << endl;

}

template<typename T>

void show(T &a)  // 这里的T代表一个具体化之后的类

{

    cout << "x = " << a.x << ", y = " << a.y << endl;

}

int main()

{

    Base<int> a(99, 999);

    print();

    show(a);

    return 0;

}

        3、模板类的非约束友元函数

    该友元函数的特点与约束友元函数相反:也就是每个类的具体化都会获得每个函数的具体化,假如具体化一个int类,在该类中仍然可以获得一个double、string、int等类的友元函数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

template<typename T>

class Base{

public:

    Base(T x, T y): x(x), y(y){}

    template<typename D>

    friend void print(Base<D> &obj);

private:

    T x;

    T y;

};

template<typename D>

void print(Base<D> &obj){

    cout << "x = " << obj.x << ", y = " << obj.y << endl;

}

int main()

{

    Base<int> a(99, 999);

    print(a);

    return 0;

}

 


原文转自:乐搏学院http://www.learnbo.com/front/article/cmsIndex
原创粉丝点击