学习笔记之使用类模板遇到的重载问题

来源:互联网 发布:剑三商城怎么导入数据 编辑:程序博客网 时间:2024/05/16 23:48

一:类模板中的运算符重载问题

在类中如果重载输入输出运算符那么在类内定义友元,在类外面定义这在类模板中是错误的,因为形如下的运算符重载

Template<typename T>

Class B

{

Public:

B(){}

Void func();

Friend ostream & operator <<(ostream & out,B<T> & data);

Private:

T ma ;

T mb;

};

Template<typename T>

Void B<T>::func()

{

Cout<<”func”;
}

Template<typename T>

Ostream & operator << (ostream & out,B<T> & data)

{

     Out<<data.ma;

Out<<data.mb;

Return out;

}

这个是错误的因为不是成员函数,所以相当于重新写了一个模板类,所以会出现链接错误。

为了解决这个问题共有一下三种方式:

1. 如果要将定义写在外面那么可以通过类的公有成员函数来实现对类的私有成员的操作,这样可以不必在类内声明为有缘,直接在类外重载即可

2. 使用过渡函数(没有看懂,貌似是套了个马甲)

3.

可以将上面的代码写成如下形式

Template<typename T>

Class B

{

Public:

B(){}

Void func();

Friend ostream & operator << <T>(ostream & out,const B<T> & data);

Private:

T ma ;

T mb;

};

Template<typename T>

Void B<T>::func()

{

Cout<<”func”;
}

Template<typename E>

Ostream & operator << (ostream & out,const B<E> & data)

{

     Out<<data.ma;

Out<<data.mb;

Return out;

}

 

 

二.类模板的友元的写法与普通类的友元有区别

类模板的友元的情况如下:主要包含六种形式:

1. 类 - 友元(friend): "模板参数是当前类"的类模板;

2. 类 - 友元: "模板参数任意"的模板类;

3. 模板类 - 友元: "模板参数相同"的模板类;

4. 模板类 - 友元: "模板参数任意"的模板类

5. 模板类 - 友元: 类;

6. 模板类 - 友元: 当前模板参数(类); (C++11)

 

 

cpp] view plain copy

 print?

1. /* 

2.  * cppprimer.cpp 

3.  * 

4.  *  Created on: 2013.11.24 

5.  *      Author: Caroline 

6.  */  

7.   

8. /*eclipse cdt, gcc 4.8.1*/  

9.   

10. #include <iostream>  

11. #include <string>  

12.   

13. template <typename T> class Pal; //pal朋友  

14.   

15. class C {  

16.     friend class Pal<C>; //"以类C实例化"PalC的友元  

17.     template <typename T> friend class Pal2; //Pal2类的所有实例化都为C的友元  

18. private:  

19.     void print() { std::cout << "class C" << std::endl; }  

20. };  

21.   

22. template <typename T>  

23. class C2 {  

24.     friend class Pal<T>; //"C2类相同实例化"PalC2的友元  

25.     //Pal2类的所有实例化都为C2的友元注意模板参数(X)不能相同  

26.     template <typename X> friend class Pal2;  

27.     friend class Pal3; //普通友元  

28.     friend T; //C++11 模板类型参数友元  

29. private:  

30.     void print() { std::cout << "class C2" << std::endl; }  

31. };  

32.   

33. template <typename T>  

34. class Pal {  

35.     C myC;  

36.     C2<T> myC2; //必须为T  

37.     //C2<double> myC2; //实例化不同无法使用  

38. public:  

39.     void printC() {  

40.         std::cout << "this is class Pal : ";  

41.         myC.print();  

42.     }  

43.     void printC2() {  

44.         std::cout << "this is class Pal : ";  

45.         myC2.print();  

46.     }  

47. };  

48.   

49. template <typename T>  

50. class Pal2 {  

51.     C myC;  

52.     C2<double> myC2;  

53. public:  

54.     void printC() {  

55.         std::cout << "this is class Pal2 : ";  

56.         myC.print();  

57.     }  

58.     void printC2() {  

59.         std::cout << "this is class Pal2 : ";  

60.         myC2.print();  

61.     }  

62. };  

63.   

64. class Pal3 {  

65.     C2<double> myC2;  

66. public:  

67.     void printC2() {  

68.         std::cout << "this is class Pal3 : ";  

69.         myC2.print();  

70.     }  

71. };  

72.   

73. class Pal4 {  

74.     C2<Pal4> myC2; //注意Pal4C2类的模板参数  

75. public:  

76.     void printC2() {  

77.         std::cout << "this is class Pal4 : ";  

78.         myC2.print();  

79.     }  

80. };  

81.   

82. int main (void) {  

83.   

84.     std::cout << "Hello Mystra!" << std::endl;  

85.   

86.     Pal<C> pc; //Pal类必须实例化为C  

87.     pc.printC(); //可以使用  

88.     //Pal<int> pci;  

89.     //pci.print(); //报错访问了私有成员  

90.   

91.     Pal2<int> pi2; //Pal2类可以随意实例化  

92.     pi2.printC();  

93.   

94.     Pal<int> pi; //有相同的示例化在类声明C2<T>  

95.     pi.printC2();  

96.   

97.     pi2.printC2(); //注意, Pal2类被实例化为<int>, 内部C2类被实例化为<double>;  

98.   

99.     Pal3 p3;  

100.     p3.printC2();  

101.   

102.     Pal4 p4;  

103.     p4.printC2();  

104.   

105.     return 0;  

106. }  

 

 

三.

0 0
原创粉丝点击