运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)

一般的我们喜欢这样对对象赋值:

Person p1;Person p2=p1;

classT object(another_object), or    A a(b);

classT object = another object;

class A

{

//  …

};

int main( )

{

A x;

A y(x);

// …

A z = x;

  z = y;

}

这样的话,如果成员变量中有指针的话,就容易造成指针的二次删除。这样就需要我们显示的在类中实现

1、拷贝构造,

2、赋值运算符重载。

     1)、判断是不是自赋值,2)、释放旧空间,3)、开辟新空间。4)、内容本身的        拷贝,5)、返回*this

3、析构函数(释放指针指向的空间)。

这三步使类实现深拷贝,从而避免浅拷贝的二次删除问题。俗称三大件。


class Vector

{

private:

   int *rep;

   int size;

   void clone(const Vector& a);

   void dispose( );

public:

   Vector(int s=0);      

   // a default constructor initializing all members of rep to 0 if s is not 0.

   Vector( int*, int );     

   // a constructor creates a Vector object from an ordinary array

   Vector(const Vector& v); // a copy constructor

   ~Vector( ) {dispose( );} // a destructor

   int get_size( ) const {return size;} // an accessor

   const Vector& operator=(const Vector& x);

   int& operator[ ](int index) {return rep[index];}

   const int& operator[ ](int index) const {return rep[index];}

};

//Vector v;

//V = a;

//Vector v(a);

void Vector::clone(const Vector& a)

{

   this->size = a.size;      rep = new int[size];

   for (int count = 0; count < size; ++count)

   rep[count] = a[count]; // (*this)[count] = a[count];

                          // rep[count] = a.rep[count];

}

void Vector::dispose( )

delete [ ] rep; 

rep = NULL;

}

Vector::Vector(int s) : size(s)

{

   if (size <= 0)

   { rep = NULL; }

   else

   {

      rep = new int[size];

      for (int count = 0; count < size; ++count)

      { rep[count] = 0; }     }

}

Vector::Vector(int* a, int s) : size(s), rep(new int[s])

{

   for (int count = 0; count < size; ++count)

   { rep[count] = a[count]; }

}

Vector::Vector(const Vector& v)

{ clone(v); }

//for example: Vector a, v; a.=(v);

const Vector& Vector::operator=(const Vector& x)

{

   if ( this != &x )  //Vector v; Vector* p = &v; v = *p;

   {

      delete []rep;

      this->size = x.size;

   rep = new int[size];

      for (int count = 0; count < size; ++count)

      rep[count] = x[count];

   }

   return *this;

}

// overloading operator <<, not a friend function

ostream& operator<<(ostream& out, const Vector& x)

{

   int s = x.get_size( );

   for (int i = 0; i < s; ++i)

   {

      out << x[i]<<endl; // out<<x.rep[i]<<endl;

   }

   out << endl;

   return out;

}

bool operator==(const Vector& a, const Vector& b)

{

   bool yes = true;

   if (a.get_size( ) != b.get_size( ))

   { yes = false; }

   else

   {

      int s, index = 0;

      s = a.get_size( );

      while (index < s && a[index] == b[index])

      { ++index; }

      if (index < s)

      { yes = false; }

   }

   return yes;

}

int main()

{

Vecter vec1;

cout<<vec1<<endl;

int array[5] = {1,2,3,4,5};

Vector vec2( array, 5 );

cout<<vec2<<endl;

Vector vec3( vec2 );  

cout<<vec3<<end;

if( vec3 == vec2 )

{

Cout<<”The vector3 is equal to vector2”<<endl;

}

Vector vec4;

vec4 = vec3;

cout<<vec4<<end;

return 0;

}



原文地址:https://www.cnblogs.com/pangblog/p/3241594.html