0

下面是一个 Vector 的模板类,它存储了不同类型的数据元素。检查复制构造函数的代码和主代码。我所期望的语句“cout << vCHAR2[2] << endl;” 应该打印值“费用”,因为复制承包商正在做一个浅拷贝,但它正在打印“责任”。

有谁能够帮助我?谢谢。

template<typename T>
class Vector{
  private:
      T* ptr;
      int size;
  public:
      Vector<T>(int s = 10){
           size = s;
           if(size!=0)
           {
               ptr = new T[size];

           }else{
               ptr = 0;
           }

      }
      Vector<T>(const Vector<T> &copy){
            this->size=copy.getSize();

            if(size !=0)
            {
                 ptr=new T[size];
                 for(int i=0;i<size;i++)
                    ptr[i] = copy.ptr[i];    
            }else{
               this->ptr=0;
            }
      }

      ~Vector<T>(){
         if(size>0)
         {
            delete[] ptr;
         }
      }
      int getSize() const
      {
          return size;    
      }
      const Vector<T> & operator = (const Vector<T> &rhs){
            if(this!=&rhs)
                 delete [] this->ptr;
                 size = rhs.size;
                 if(size!=0)
                 {
                      ptr=new T[size];
                      for(int i=0;i<size;i++)
                              ptr[i] = rhs.ptr[i];
            }

            return *this;

      }

      T& operator[](int index){
         if(index>=0 && index<=size)
            return ptr[index];
      }
};    




int main(int argc, char *argv[])
{
  Vector<char*> vCHAR(10);
  vCHAR[0]="asset";
  vCHAR[1]="income";
  vCHAR[2]="liability";

  Vector<char*> vCHAR2(vCHAR);
  vCHAR[2] = "expense";

  cout << vCHAR[2] << endl;

  cout << vCHAR2[2] << endl;

  system("PAUSE");
  return EXIT_SUCCESS;
}
4

4 回答 4

0

复制 ctor 进行深度复制,因此 vCHAR2 有自己的元素数组。因此,当您更改源 Vector 的元素时,不需要查看。(当您更改通过 strcpy() 或访问指向的数据时,它会看到它vCHAR[2][0]='X';(前提是这不会使您的程序崩溃-当您对字符串文字进行操作时))

于 2012-07-06T08:28:33.840 回答
0

with vCHAR[2] = "expense" you are changing the pointer inside the vector vCHAR, but the vCHAR2[2] still point to the old location. In short - there is no shallow copy of a pointer. If you'd re-used T* from the source when copying the vector, you'd have what you wanted.

于 2012-07-06T08:28:37.397 回答
0

The design you have chosen to implement a vector is dangerous. If you have decided to manage elements using a kind of the node allocation approach (whereas std::vector uses the block allocation approach) you have to be careful with the pointers and memory management routines. The issues you have got are related to how you work with pointers: in T* ptr; and T& operator[], including the array copy routine. In your example, you are working with a pointer-to-pointer to char - char** (substitute the template with char*). If you are decided to design your own vector implementation with the node allocation approach, I would suggest to also implement the struct VectorTraits and design your vector class using it, at least. Also I would suggest to use std::string instead of char*.

于 2012-07-06T09:32:18.063 回答
0

In the line:

Vector<char*> vCHAR2(vCHAR);

vCHAR2[2] is a pointer to the string "liability". The line

vCHAR[2] = "expense";

does not change the value of vCHAR2[2] because vCHAR2[2] is still pointing to "liability" even if vCHAR[2] has changed.

To change it you just have to assign to it directly ie

vCHAR2[2] = "expense";

I think what you are trying to achieve is something along the lines of:

int* p = new int();
*p = 111;

int* q = p;

*p = 222; // change the content of what is pointed to

cout << *p << endl; // 222
cout << *q << endl; // 222 also

However, this is a different case since we are changing the contents of what is pointed to. If we just do pointer assignment, the contents are unchanged. Only the pointer is assigned to another area of memory.

*p = 111;

int* q = p;

int z = 333;
p = &z; // do not change the content of what is pointed to, point to another area

cout << *p << endl; // 333
cout << *q << endl; // 222 still
于 2012-07-06T09:37:48.117 回答