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

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;

}

运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)的更多相关文章

  1. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  2. [置顶] 运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)

    一般的我们喜欢这样对对象赋值: Person p1;Person p2=p1; classT object(another_object), or    A a(b); classT object = ...

  3. 浅拷贝(Shallow Copy) VS 深拷贝(Deep Copy)

    首先,深拷贝和浅拷贝针对的是对象类型(对象,数组,函数) 浅拷贝指的是只是拷贝了对象的引用地址,彼此之间高耦合,一个改变,另一个可能也随之改变: 深拷贝是指只是完整的将变量的值拷贝过来,是一个新的对象 ...

  4. 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考

    首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME    copy - Generic (shallow and dee ...

  5. python(41):copy拷贝(深拷贝deepcopy与浅拷贝copy)

    Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1.copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2.copy.deepco ...

  6. Python浅拷贝copy()与深拷贝deepcopy()区别

    其实呢,浅拷贝copy()与深拷贝deepcopy()之间的区分必须要涉及到python对于数据的存储方式. 首先直接上结论: —–我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的 ...

  7. copy&mutableCopy 浅拷贝(shallow copy)深拷贝 (deep copy)

    写在前面 其实看了这么多,总结一个结论: 拷贝的初衷的目的就是为了:修改原来的对象不能影响到拷贝出来得对象 && 修改拷贝出来的对象也不能影响到原来的对象 所以,如果原来对象就是imm ...

  8. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  9. C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)

    运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...

随机推荐

  1. Python并发编-用Event,线程检测数据库连接的例子

    尝试3次连接数据库 import time import random from threading import Thread,Event def connect_db(e): count = 0 ...

  2. Linux下定时备份MySQL数据库的Shell脚本

    Linux下定时备份MySQL数据库的Shell脚本   对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...

  3. JSTL-1

    JSTL的配置和使用: * 配置:将jstl.jar和standard.jar拷贝到WEB-INF/lib下 * 使用:要采用一些指令:采用taglib指令 JSTL标准标签库(JSP Standar ...

  4. hdu 2433 Travel(还不会)

    Problem Description       One day, Tom traveled to a country named BGM. BGM is a small country, but ...

  5. SPOJ1811 && SPOJ1812

    SPOJ1811 && SPOJ1812 LCS && LCS2 非常神奇的两道题... 题目大意: 给定n个字符串,求最长公共子串 做法1: 后缀数组: 把字符串连起 ...

  6. 2017-2018-1 JAVA实验站 冲刺 day07

    2017-2018-1 JAVA实验站 冲刺 day07 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 写博客.进行工作总结 100% 齐力锋 部分按钮图片.对按钮图片进行ps 100 ...

  7. bzoj 1018 线段树维护连通性

    本题将一道LCT的题特殊化(支持加边和删边,询问图的连通性),将图变成了2×m的网格图,然后就神奇地可以用线段树来维护. 对于每个区间[l,r],维护其四个角落之间的连通性(仅仅通过[l,r]这段的边 ...

  8. bzoj 2055: 80人环游世界 -- 上下界网络流

    2055: 80人环游世界 Time Limit: 10 Sec  Memory Limit: 64 MB Description     想必大家都看过成龙大哥的<80天环游世界>,里面 ...

  9. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance 模拟

    D. Little Artem and Dance 题目连接: http://www.codeforces.com/contest/669/problem/D Description Little A ...

  10. HDU 5645 DZY Loves Balls 水题

    DZY Loves Balls 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5645 Description DZY loves playing b ...