运算符重载,浅拷贝(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;
}
运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)的更多相关文章
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
- [置顶] 运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)
一般的我们喜欢这样对对象赋值: Person p1;Person p2=p1; classT object(another_object), or A a(b); classT object = ...
- 浅拷贝(Shallow Copy) VS 深拷贝(Deep Copy)
首先,深拷贝和浅拷贝针对的是对象类型(对象,数组,函数) 浅拷贝指的是只是拷贝了对象的引用地址,彼此之间高耦合,一个改变,另一个可能也随之改变: 深拷贝是指只是完整的将变量的值拷贝过来,是一个新的对象 ...
- 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考
首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME copy - Generic (shallow and dee ...
- python(41):copy拷贝(深拷贝deepcopy与浅拷贝copy)
Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1.copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2.copy.deepco ...
- Python浅拷贝copy()与深拷贝deepcopy()区别
其实呢,浅拷贝copy()与深拷贝deepcopy()之间的区分必须要涉及到python对于数据的存储方式. 首先直接上结论: —–我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的 ...
- copy&mutableCopy 浅拷贝(shallow copy)深拷贝 (deep copy)
写在前面 其实看了这么多,总结一个结论: 拷贝的初衷的目的就是为了:修改原来的对象不能影响到拷贝出来得对象 && 修改拷贝出来的对象也不能影响到原来的对象 所以,如果原来对象就是imm ...
- PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天
PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数 1 构造 2 析构 3 赋值 4 拷贝构造 5 oper ...
- C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)
运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...
随机推荐
- [实战]MVC5+EF6+MySql企业网盘实战(13)——编辑文件夹
写在前面 上篇文章实现了,新建文件夹以及与之前的上传文件的逻辑做了集成,本篇文章将实现编辑文件夹名称,其实这个也有难点,就是编辑文件夹名称时,要考虑文件夹中存在文件或者子文件夹的情况,因为他们的路径已 ...
- Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp
C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...
- bzoj 1108
思路:水题, 将所有点按x轴对称反转,就变成了两堆点的坐标和的差.. #include<bits/stdc++.h> #define LL long long #define fi fir ...
- 使用 Java 查找字符串中出现次数最多的字符以及出现的次数?
使用 Java 查找字符串中出现次数最多的字符以及出现的次数? import java.util.HashMap; import java.util.Map; public class TestStr ...
- 微信公共服务平台开发(.Net的实现)1 认证“成为开发者”
http://www.cnblogs.com/freeliver54/p/3725979.html http://www.it165.net/pro/html/201402/9459.html 这些代 ...
- java 数组冒泡排序、转置(降序)
1.java 数组冒泡排序 排序的基本原理(升序): 原始数据: 2 .1 .9 .0 .5 .3 .7 .6 .8: 第一次排序: 1 .2 .0 .5 .3 .7 .6 .8 .9 : 第二次 ...
- /.nav-tabs :是普通标签页 .nav-pills:胶囊式标签页 action ;默认的激活项,给<li>加默认显示的是哪个标签页内容 .nav是标签页的一个基类,给ul加 .nav-stacked: 垂直排列BootStrap
<meta name="viewport" content="with=device-width, initial-scale=1, user-scalabe=no ...
- ubuntu16.04.2安装完后重启报错[sda] Assuming drive cache: write through
原因:检测主机的物理连接线,发生问题时"已连接"未勾选,重启的时候找不到iso文件 解决办法:勾选"已连接",重启机器成功
- BZOJ 4566 JZYZOJ 1547 [haoi2016T5]找相同子串 后缀数组 并查集
http://172.20.6.3/Problem_Show.asp?id=1547 http://www.lydsy.com/JudgeOnline/problem.php?id=4566 单纯后缀 ...
- java--由一道选择题研究数值越界
原题来自牛客网的java专项练习: 以下是代码. public class Test2 { public static void add(Byte b) { b=b++; } public stati ...