运算符重载,浅拷贝(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;

}

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值