实现数组类(C++ 拷贝构造函数、拷贝函数)要判断赋值左右对象不相等,坑惨了
#include <iostream>
using namespace std;
class ArrayIndexOutOfBoundsException{ // 异常类
public:
int index;
ArrayIndexOutOfBoundsException(int k){
index = k;
}
};
class Array{
private:
int *data;
int size;
static const int dSize = ; // 数组默认大小
public:
Array( ){ // 无参构造
size = dSize;
data = new int[size]( );
} Array(int n ){ // 有参构造
size = n;
data = new int[size]( );
} Array(const Array& arr)//拷贝构造函数,深拷贝
{
if(arr.size>)
{ size = arr.size;
data = new int[size]( );
for (int i = ; i < size; i++)
{
data[i] = arr.data[i];
}
}
} Array& operator = (const Array& arr)
{
if(this!=&arr)//如果等号右侧的对象和左边的不是一个对象再赋值(没有这句会运行error),要判断赋值左右对象不相等,坑惨了
{
delete []data;//先释放掉之前的内存,否则会内存超限
size = arr.size;
data = new int[size]( );
for (int i = ; i < size; i++)
{
this->data[i] = arr.data[i];
}
} return *this;
} ~Array()
{
if (this->data != NULL)//不为空才释放
{
delete []data;
} } int& operator [] (int k){ // 运算符 [ ] 重载,以方便数组的使用
if(k< || k>=size) throw ArrayIndexOutOfBoundsException(k);
return data[k];
}
friend ostream& operator << (ostream& o, const Array& a); // 运算符 << 重载,以方便输出
};
ostream& operator << (ostream& o, const Array& a){
o << '[' ;
for(int i=; i<a.size-; i++)
o << a.data[i] << ',' ;
o << a.data[a.size-] << ']';
return o;
}
// 注意:实际测试程序中,在此处之前的代码与样例中相同
// 注意:实际测试程序中,在此处之后的代码(即main函数)可能与样例中不同
int main(){
int n, k;
cin >> n >> k;
Array a(n); // 构造数组,大小为 n
for(int i=; i<n; i++) a[i] = i;
Array b = a; // 拷贝构造数组
b[n/] = k;
cout << a << endl;
cout << b << endl;
Array c; // 构造数组,默认大小
c = a; // 拷贝数组
c[n/] = k;
cout << a << endl;
cout << c << endl;
a = a;
a[n/] = ;
cout << a << endl;
return ;
}
实现数组类(C++ 拷贝构造函数、拷贝函数)要判断赋值左右对象不相等,坑惨了的更多相关文章
- [c++基础]3/5原则--拷贝构造函数+拷贝赋值操作符
/* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> #include &qu ...
- c++面向对象 之 基础 类修饰符 构造函数 友元函数
1,类和对象 定义一个类,本质上是定义一个数据类型的蓝图.这实际上并没有定义任何数据,但它定义了类的名称意味着什么,也就是说,它定义了类的对象包括了什么,以及可以在这个对象上执行哪些操作. 类定义格式 ...
- 拷贝构造函数 & 拷贝赋值运算符
一.拷贝构造函数 1. 形式 class A { public: // ... A(const A &); // 拷贝构造函数 }; 2. 合成拷贝构造函数 编译器总会为我们合成一个拷贝构造函 ...
- c++类大四个默认函数-构造函数 析构函数 拷贝构造函数 赋值构造函数
每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含一个拷贝构造函数,其它的称为普通构造函数).对于任意一个类A,如果不编写上述函数,C++编译器将自动为A 产生四个缺省的函数,例如: A ...
- CPP_类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数
类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数 // person.h #ifndef _PERSON_H_ #define _PERSON_H_ class Person{ public : ...
- C++ 类 & 对象-类成员函数-类访问修饰符-C++ 友元函数-构造函数 & 析构函数-C++ 拷贝构造函数
C++ 类成员函数 成员函数可以定义在类定义内部,或者单独使用范围解析运算符 :: 来定义. 需要强调一点,在 :: 运算符之前必须使用类名.调用成员函数是在对象上使用点运算符(.),这样它就能操作与 ...
- 《剑指offer》面试题1:为类CMyString添加赋值运算符函数——C++拷贝构造函数与赋值函数
题中已给出CMyString的类定义,要求写赋值运算符函数. #include<iostream> #include<cstring> using namespace std; ...
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
一.题目: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &othe ...
- C++中构造函数,拷贝构造函数和赋值函数的区别和实现
C++中一般创建对象,拷贝或赋值的方式有构造函数,拷贝构造函数,赋值函数这三种方法.下面就详细比较下三者之间的区别以及它们的具体实现 1.构造函数 构造函数是一种特殊的类成员函数,是当创建一个类的对象 ...
随机推荐
- python打印日历
#未优化的代码 1 #输出日历 def print_calendar(year,month,date = 1): month_dict = {':'July', ':'December'} #数字月份 ...
- C# DataGridView 使用
之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件. 现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了. 我现在要是设置两列 ...
- php 微擎
pdo_insert('ewei_shop_member', $data); $my = array('agentid' => '4102'); // pdo_update(表明,'修改的值', ...
- 使用SQLAlchemy对博客文章进行分页
https://blog.csdn.net/hyman_c/article/details/54382161
- Java基础学习总结(39)——Log4j 1使用教程
1. 配置文件 Log4J配置文件的基本格式如下: #配置根Logger log4j.rootLogger = [ level ] , appenderName1 , appenderN ...
- Bi-shoe and Phi-shoe 欧拉函数 素数
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...
- [Javascript Crocks] Flatten Nested Maybes with `chain`
Sometimes, we run into situations where we end up with a Maybe within the context of another Maybe. ...
- [React] Forward a DOM reference to another Component using forwardRef in React 16.3
The function forwardRef allows us to extract a ref and pass it to its descendants. This is a powerfu ...
- C算法与数据结构-线性表的应用,多项式求和---ShinePans
/*---上机作业作业,二项式加法---*/ /*---By 潘尚 ---*/ /*---日期: 2014-5-8 . ---*/ /*---题目:---*/ //如果有两个稀疏多项式A和B,设计算法 ...
- Hibernate 自定义方言
自定义一个方言类——Hibernate Dialect 标签: hibernatesqlserverjdbcmysql数据库java 2012-07-04 18:46 2847人阅读 评论(1) 收藏 ...