复制随机值后的c ++拷贝构造函数

时间:2014-04-06 19:20:14

标签: c++

我也有一些我无法弄清楚复制构造函数的错误,但目前的主要问题是在我的测试程序评论中提出的。为什么第二次打印出这么大的数字,而不是三?提前感谢您的帮助。

template<class T>
class Array {
    private:
        T *cont;
        unsigned int size;

    public:

    Array()=default;


    T& elementAt(unsigned int i)
    {
    return cont[i]; 
    }


    Array( const Array &a ) 
    {
    cont = new int[ size ];

    for ( int i = 0; i < size; i++ )
        cont[ i ] = a.cont[ i ];
    }


    Array& operator = (const Array& a)
    {
    int i,min_size;
    if(size < a.size)
        min_size = size;
    else
        min_size = a.size;
    for(i=0; i<min_size; i++)
        cont[i] = a.cont[i];
    return (*this);
    }


    void addElement(T element) {
        T *new_cont = new T[size + 1], *old_cont = cont;

        for (int i = 0; i < size; i++) {
            new_cont[i] = old_cont[i];
        }

        new_cont[size] = element;
        size++;

        cont = new_cont;
        delete old_cont;
    }


    unsigned int getSize() const
    {
    return size;
    }


    ~Array()
    {
    delete [] cont;
    }



};

这部分测试程序:

Array<int> numbers;             
    numbers.addElement (5);         
    numbers.addElement (11);        
    numbers.addElement (3);
        cout << numbers.getSize()<<endl;  // Here I get output: 3
    cout<< numbers.elementAt(1)<<endl; // output: 11
        MyArray<int> copy2;
    copy2 = numbers;    
        cout << copy2.getSize()<<endl;  //  WHY is output: 2686697, in here? Why isn' t it 3.
    cout<< copy2.elementAt(1)<<endl; // output: 11

3 个答案:

答案 0 :(得分:1)

在您调用复制构造函数时,size成员未初始化。你可能意味着

 size = a.size; // Initialize size member also, it's not done automagically
 cont = new int[ size ]; 

在你的拷贝构造函数中。

答案 1 :(得分:0)

你应该从对象a

获得大小
Array( const Array &a ) 
{
    cont = new int[ a.size ];

    for ( int i = 0; i < a.size; i++ )
        cont[ i ] = a.cont[ i ];
}

答案 2 :(得分:0)

您使用错误的size在复制构造函数中分配内存。

Array( const Array &a ) 
{
  // Get the size from the copy first. Without it, size is an unitialized
  // value, which will lead to undefined behavior.
  size = a.size;
  cont = new int[ size ];

  for ( int i = 0; i < size; i++ )
    cont[ i ] = a.cont[ i ];
}

我看到的另一个问题是默认构造函数的实现。默认构造函数可以以两种方式使用:

Array<int> numbers;

Array<int> numbers = Array<int>();

前者将导致未初始化的数据,而后者将导致您的实施正确初始化数据。详情请见here

您可以将默认构造函数更改为:

Array() : size(0), cont(NULL) {}

使代码在构造对象的任何一种方式下都能完全相同。

第三个问题是operator=功能。它不执行真正的分配。它仅存储两个对象中的最小元素数。试试这个:

   Array& operator = (const Array& a)
   {
      // Make statements like 'a = a;' a noop.
      if ( this != &a )
      {
         // If the size of the LHS is larger than or equal to the size
         // of the RHS, the LHS has enough memory to store the data
         // from the RHS.
         if ( this->size >= a.size )
         {
            this->size = a.size;
            for(int i=0; i<a.size; i++)
               cont[i] = a.cont[i];
         }
         else
         {
            // The size of the LHS is smaller than the size of the RHS,
            // It does not have enough memory to store the data from
            // the RHS. Dellocate old memory. Allocate new memory.
            // Then copy the data.
            this->size = a.size;
            delete [] cont;
            cont = new int[this->size];
            for(int i=0; i<a.size; i++)
               cont[i] = a.cont[i];

         }
      }
      return (*this);
   }