在C中实现虚拟析构函数

前端之家收集整理的这篇文章主要介绍了在C中实现虚拟析构函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始学习c但是我陷入了析构函数.我们需要实现一个向量,这是我到目前为止所拥有的.
  1. #include<string.h>
  2. #include<cassert>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6. template<class T>
  7. class Vector {
  8. template<class U> friend ostream& operator<<(ostream&,const Vector<U>&);
  9. private:
  10. T* data;
  11. unsigned len;
  12. unsigned capacity;
  13. public:
  14. Vector(unsigned = 10);
  15. Vector(const Vector<T>&);
  16. virtual ~Vector(void);
  17. Vector<T>& operator =(const Vector<T>&);
  18. bool operator==(const Vector<T>&);
  19. T& operator[](unsigned);
  20. };
  21.  
  22. //PROBLEM!
  23. template <class T>
  24. ~ Vector() {
  25. delete data;
  26.  
  27. }
  28.  
  29. template<class T>
  30. Vector<T>::Vector(unsigned int _capacity)
  31. {
  32. capacity = _capacity;
  33. len = _capacity;
  34. data = new T[_capacity];
  35. }
  36.  
  37. template<class T>
  38. Vector<T>::Vector(const Vector<T> & v)
  39. {
  40. len = v.len;
  41. capacity = v.capacity;
  42. data = new T[len];
  43. for (unsigned int i = 0; i < len; i++)
  44. data[i] = v.data[i];
  45. }
  46.  
  47.  
  48.  
  49. template<class T>
  50. Vector<T> & Vector<T>::operator = (const Vector<T> & v)
  51. {
  52. delete[ ] data;
  53. len = v.len;
  54. capacity = v.capacity;
  55. data = new T [len];
  56. for (unsigned int i = 0; i < len; i++)
  57. data[i] = v.data[i];
  58. return *this;
  59. }
  60.  
  61. template<class T>
  62. bool Vector<T>::operator == (const Vector<T> & v)
  63. {
  64. bool check = true;
  65. check &= (len == v.len);
  66. if (!check) return false;
  67. check &= (capacity == v.capacity);
  68. if (!check) return false;
  69. for (unsigned int i = 0; i < len; i++) {
  70. check &= (data[i] == v.data[i]);
  71. if (!check) return false;
  72.  
  73. }
  74. return true;
  75. }
  76.  
  77. template<class T>
  78. T& Vector<T>::operator[](unsigned int index)
  79. {
  80. return data[index];
  81. }

给出了接口,我需要实现它.但这与C和Java有很大不同,我有点迷失.

在第二个练习中,我们需要使用a)前面的Vector实现作为派生类和b)Vector作为组合类来实现这样的东西,所以我们可能会在其中一种方法中使用虚拟析构函数

  1. void testAssociativeArray() {
  2. AssociativeArray<String,int> table;
  3. table["abc"] = 15;
  4. table["jkl"] = 12;
  5. table["xyz"] = 85;
  6. assert(table["jkl"],12);
  7. }
  8.  
  9. template<class P,class Q>
  10. class Pair {
  11. P p;
  12. Q q; public:
  13. Pair(const P& _p = P(),const Q& _q = Q()): p(_p),q(_q) {}
  14. P& objectP() {return p;}
  15. Q& objectQ() {return q;}
  16. };

解决方法

首先,为什么你认为析构函数应该是虚拟的?你在使用多态吗?

其次,您正在使用不正确的数组删除.

既然你用过:

  1. data = new T[length];

您必须使用数组语法:

  1. delete [] data;

第三,您需要将命名空间放在所有类函数定义的前面:

  1. template <class T>
  2. Vector<T>::~Vector()
  3. {
  4. delete [] data;
  5. }

只是为了您的信息,您声明析构函数是这样的……

  1. virtual ~Vector(void);

正如我所提到的,虚拟是不必要的,除非您以多态方式将此类用作基类或派生类.有关何时需要使用虚拟析构函数的更多信息,请查看answer to this question.

另外,参数中的空隙也是不必要的.这在旧C标准中曾经是必需的,但它不在C中.

您应该可以这样声明它:

  1. ~Vector();

如果您定义AssociativeArray< P,Q>通过与Vector< T>的has-a关系,你可以简单地使该类包含Vector< Pair< P,Q>取代.在这种情况下声明虚拟方法不是必需的,但仍然可以使用 – 带来一些额外的开销.

如果您定义AssociativeArray< P,Q>具有与Vector< Pair< P,Q>的关系的is-a. >,然后您应该在Vector< T>中定义一些虚拟方法,包括虚拟析构函数.

虚拟方法的使用仅在通过指针和引用以多态方式使用对象时才有意义.见this page.

  1. AssociativeArray<String,Int>* myDerivedMap = new AssociativeArray<String,Int>();
  2. delete myDerivedMap; //NO virtual methods necessary here. using a pointer to derived class
  3.  
  4. Vector<Pair<String,Int> >* myBaseMap = new AssociativeArray<String,Int>();
  5. delete myBaseMap; //virtual methods ARE necessary here. using a pointer to base class

猜你在找的C&C++相关文章