设为首页 加入收藏

TOP

8.7.2 根据类模板创建对象(1)
2013-10-07 16:09:06 来源: 作者: 【 】 浏览:64
Tags:8.7.2 根据 模板 创建 对象

8.7.2  根据类模板创建对象(1)

当使用函数模板定义的函数时,编译器能够根据函数实参的类型推断出模板类型实参。函数模板的类型形参是使用特定的函数隐式确定的。类模板有些不同。为了以类模板为基础创建对象,必须在声明中指定类名后面的类型形参。

例如,为了声明一个CSamples< >对象来处理double类型的样本,需要将声明写成下面这样:

  1. CSamples<double> myData(10.0); 

该语句定义了一个CSamples<double>类型的对象,它可以存储double类型的样本。该对象是用值为10.0的一个样本创建的。

试一试:使用类模板

可以根据CSamples< >模板,创建一个存储CBox对象的对象。这是没有问题的,因为CBox类实现了重载大于运算符的operator>()函数。利用下面代码中给出的main()函数,我们可以练习类模板的用法:
 

  1. // Ex8_10.cpp  
  2. // Using a class template  
  3. #include <iostream> 
  4. #include <utility>                                                                                                      // For operator overload templates  
  5. using std::cout;  
  6. using std::endl;  
  7. using namespace std::rel_ops;  
  8. // Put the CBox class definition from Ex8_06.cpp here...  
  9. // CSamples class template definition  
  10. template <class T> class CSamples  
  11. {  
  12. public:  
  13. // Constructors  
  14. CSamples(const T values[], int count);  
  15. CSamples(const T& value);  
  16. CSamples(T&& value);  
  17. CSamples() : m_Free(0) { }  
  18. bool Add(const T& value);                                                               // Insert a value  
  19. bool Add(T&& value);                                                                                // Insert a value with move semantics  
  20. T Max() const;                                                                                                  // Calculate maximum  
  21. private:  
  22. static const size_t maxSamples = 100;                       // Maximum number od sample  
  23. T m_Values[maxSamples];                                                                     // Array to store samples  
  24. int m_Free;                                                                                                             // Index of free location in m_Values  
  25. };  
  26. // Constructor template definition to accept an array of samples  
  27. template<class T> CSamples<T>::CSamples(const T values[], int count)  
  28. {  
  29. m_Free = count < 100   count : 100;                                 // Don't exceed the array  
  30. for(int i = 0; i < m_Free; i++)  
  31. m_Values[i] = values[i];                                                                    // Store count number of samples  
  32. }  
  33. // Constructor to accept a single sample  
  34. template<class T> CSamples<T>::CSamples(T& value)  
  35. {  
  36. m_Values[0] = value;                                                                                        // Store the sample  
  37. m_Free = 1;                                                                                                                     // Next is free  
  38. }  
  39. // Constructor to accept a temporary sample  
  40. template<class T> CSamples<T>::CSamples(T&& value)  
  41. {  
  42. cout << "Move constructor." << endl;  
  43. m_Values[0] = std::move(value);                                                 // Store the sample  
  44. m_Free = 1;                                                                                                                     // Next is free  
  45. }  
  46. // Function to add a sample  
  47. template<class T> bool CSamples<T>::Add(const T& value)  
  48. {  
  49. cout << "Add." << endl;  
  50. bool OK = m_Free < 100;                                                                             // Indicates there is a free place  
  51. if(OK)  
  52. m_Values[m_Free++] = value;                                                     // OK true, so store the value  
  53. return OK;  
  54. }  
  55. template<class T> bool CSamples<T>::Add(T&& value)  
  56. {  
  57. cout << "Add move." << endl;  
  58. bool OK = m_Free < 100;                                                                             // Indicates there is a free place  
  59. if(OK)  
  60. m_Values[m_Free++] = std::move(value);                  // OK true, so store the value  
  61. return OK;  
  62. }  
  63. // Function to obtain maximum sample  
  64. template<class T> T CSamples<T>::Max() const  
  65. {  
  66. theMax = m_Values[0];                                                                             // Set first sample as maximum  
  67. for(int i = 1; i < m_Free; i++)                                                 // Check all the samples  
  68. if(theMax < m_Values[i])  
  69. theMax = m_Values[i];                                                                   // Store any larger sample  
  70. return theMax;  
  71. }  
  72. int main()  
  73. {  
  74. CBox boxes[] = {                                                                                                        // Create an array of boxes  
  75. CBox(8.0, 5.0, 2.0),                            // Initialize the boxes...  
  76. CBox(5.0, 4.0, 6.0),  
  77. CBox(4.0, 3.0, 3.0)  
  78. };  
  79. // Create the CSamples object to hold CBox objects  
  80. CSamples<CBox> myBoxes(boxes, _countof(boxes));  
  81. CBox maxBox = myBoxes.Max();                                                            // Get the biggest box  
  82. cout    << endl                                                                                                                 // and output its volume  
  83. << "The biggest box has a volume of "  
  84. << maxBox.Volume()  
  85. << endl << endl;  
  86. CSamples<CBox> moreBoxes(CBox(8.0, 5.0, 2.0));  
  87. moreBoxes.Add(CBox(5.0, 4.0, 6.0));  
  88. moreBoxes.Add(CBox(4.0, 3.0, 3.0));  
  89. cout    << "The biggest box has a volume of "  
  90. << moreBoxes.Max().Volume()  
  91. << endl;  
  92. return 0;  
  93. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.7.1 定义类模板(2) 下一篇8.7.2 根据类模板创建对象(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)