new、delete、析构函数、自动类型转换
new
分配内存,返回指针
new 类型名T (初值列表)
功能:申请用于存放T类型对象的内存空间,并依初值列表赋以初值
结果值:
成功->T类型的指针,指向新分配的内存
失败->0(NULL)
int *pl = new int;
int *pl = new int(10);
注意与malloc的区别
malloc(m):开辟m字节长度的地址空间,并返回这段空间的首地址。
sizeof(x):计算变量x的长度。
free(p):释放指针p所指变量的存储空间,即彻底删除一个变量。
delete
功能:释放指针P所指向的内存。P必须是new操作的返回值。
delete pl;
析构函数
如果构造函数使用new来分配内存,则析构函数将使用delete来释放这些内存。如果构造函数没有使用new,析构函数实际上没有需要完成的任务。在这种情况下,只需要让编译器生成一个什么都不需要做的隐式析构函数即可。
通常由编译器决定什么时候调用析构函数,代码中不会显式的调用析构函数(当然也会存在例外的情况)。如果创建的是静态存储对象,则其析构函数将在程序结束时自动调用。如果创建的是自动存储类对象,则其析构函数将在程序执行完代码块时自动被调用。如果是通过new创建的,则它将驻留在栈内存或自由存储区中,当使用delete来释放内存时,其析构函数将自动被调用。最后,程序可以创建临时对象来完成特定的操作,在这种情况下,程序将在结束对该对象的使用时自动调用其析构函数。
由于在类对象过其实析构函数将被自动调用,因此必须有一个析构函数。如果程序员没有提供析构函数,编译器将隐式地声明一个默认析构函数,并在发现导致对象被删除的代码后,提供默认析构函数的定义。
#include <cstring>
#include <iostream> using namespace std; class Point {
int x, y; public:
// constructor
Point(int _x = 0, int _y = 0);
// deconstructor
~Point();
// cout << overload
friend ostream& operator<<(ostream& os, const Point& p);
}; Point::Point(int _x, int _y) {
this->x = _x;
this->y = _y;
cout << "\nPoint is called!";
} Point::~Point() { cout << "\n~Point is called!"; } ostream& operator<<(ostream& os, const Point& p) {
cout << "(" << p.x << "," << p.y << ")";
return os;
} template <typename T>
class DynamicArray {
private:
T* array; // pointer
unsigned int mallocSize; // the length of dynamic array public:
// Constructors
// mallocSize=length, and the new element is content
DynamicArray(unsigned length, const T& content); // Destructors
~DynamicArray(); // Copy Constructor
DynamicArray(const DynamicArray<T>& anotherDA); // return the this->mallocSize
unsigned int capacity() const; // for the array[i]=someT.
T& operator[](unsigned int i); DynamicArray<T>& operator=(const DynamicArray<T>& anotherDA);
}; template <typename T>
DynamicArray<T>::DynamicArray(unsigned length, const T& content) {
this->mallocSize = length;
cout << endl
<< "new T[" << this->mallocSize << "] malloc " << this->mallocSize
<< "*" << sizeof(T) << "=" << this->mallocSize * sizeof(T)
<< " bytes memory in heap";
this->array = new T[this->mallocSize];
for (int i = 0; i < length; ++i) {
this->array[i] = content;
}
}; template <typename T>
DynamicArray<T>::~DynamicArray() {
cout << endl
<< "delete[] array free " << this->mallocSize << "*" << sizeof(T)
<< "=" << this->mallocSize * sizeof(T) << " bytes memory in heap";
delete[] array;
}; template <typename T>
DynamicArray<T>::DynamicArray(const DynamicArray<T>& anotherDA) {
// this = anotherDA;
cout << endl << "Copy Constructor is called";
this->mallocSize = anotherDA.mallocSize;
this->array = new T[this->mallocSize];
for (int i = 0; i < this->mallocSize; ++i)
this->array[i] = anotherDA.array[i];
}; template <typename T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray<T>& anotherDA) {
cout << endl << "operator = is called";
if (this == &anotherDA) return *this;
if (this->array) delete[] this->array;
this->mallocSize = anotherDA.mallocSize;
this->array = new T[this->mallocSize];
for (int i = 0; i < this->mallocSize; ++i)
this->array[i] = anotherDA.array[i];
return *this;
} template <typename T>
unsigned int DynamicArray<T>::capacity() const {
return this->mallocSize;
} template <typename T>
T& DynamicArray<T>::operator[](unsigned int i) {
return this->array[i];
} // StudybarCommentBegin
int main() {
int length, i;
cin >> length; DynamicArray<Point> iarray(length, Point(3)); DynamicArray<Point> iarray2(iarray), iarray3(iarray2); cout << endl;
for (i = 0; i < length; i++) {
cout << iarray3[i] << " ";
iarray[i] = Point(i, i + 1);
}
iarray3 = iarray2 = iarray;
cout << endl;
for (i = 0; i < length; i++) {
cout << iarray3[i] << " ";
} return 0;
}
// StudybarCommentEnd
new、delete、析构函数、自动类型转换的更多相关文章
- C++程序设计方法3:禁止自动类型转换
禁止自动类型转换 explicit #include <iostream> using namespace std; class Src;//前置类型声明,因为在Dst中要用到Src的类 ...
- JavaScript系列文章:自动类型转换-续
在上一篇文章中,我们详细讲解了JavaScript中的自动类型转换,由于篇幅限制,没能覆盖到所有的转换规则,这次准备详细讲解一下. 上次我们提到了对象类型参与运算时转换规则: 1). 在逻辑环境中执行 ...
- JavaScript系列文章:自动类型转换
我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...
- struts基于ognl的自动类型转换需要注意的地方
好吧,坎坷的过程我就不说了,直接上结论: 在struts2中使用基于ognl的自动类型转换时,Action中的对象属性必须同时添加get/set方法. 例如: 客户端表单: <s:form ac ...
- 慕课网-安卓工程师初养成-2-9 Java中的自动类型转换
来源:http://www.imooc.com/code/1236 在 Java 程序中,不同的基本数据类型的数据之间经常需要进行相互转换.例如: , 代码中 int 型变量 score1 可以直接为 ...
- 【转】JavaScript系列文章:自动类型转换
我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...
- c语言的自动类型转换
转自c语言的自动类型转换 自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.如 ...
- C语言自动类型转换
自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.(eg:int型和long型运算时,先把int量转成lo ...
- python小白——进阶之路——day2天-———变量的缓存机制+自动类型转换
# ###同一文件,变量的缓存机制 ''' -->Number 部分 1.对于整型而言,-5~正无穷范围内的相同值 id一致 2.对于浮点数而言,非负数范围内的相同值 id一致 3.布尔值而言, ...
- js 自动类型转换
js自动类型转换 1.==符号在判断左右两边数据是否相等时,如果数据类型一致,直接比较值即可 2.==符号左右数据类型不一致时,如果两方数据为布尔型.数值型.字符串型时,不为数字的那一方自动调用Num ...
随机推荐
- JUnit5学习之五:标签(Tag)和自定义注解
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 采用lua脚本获取mysql、redis数据以及jwt的校验
一.安装配置Openresty 1,安装 wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz # 下载 tar xzvf ...
- OAuth2.0安全设计之Authorization Code
OAuth 2.0 有 4 种认证流程: 授权码模式(authorization code) 简化模式(implicit) 密码模式(resource owner password credentia ...
- docker封装nuxt项目使用jenkins发布
一.概述 vue项目可以打一个dist静态资源包,直接使用Nginx发布即可. 但是nuxt项目无法像vue那样,可以打一个dist静态资源包. 需要安装Node.js,并使用npm install ...
- Python3.x 基础练习题100例(41-50)
练习41: 题目: 模仿静态变量的用法. 程序: def varfunc(): var = 0 print('var = %d' % var) var += 1 if __name__ == '__m ...
- Redis-第十章节-链表
目录 数组和链表 链表 对比 总结 1.数组和链表 数组: 数组会在内存中开辟一块连续的空间存储数据,这种存储方式有利也有弊端.当获取数据的时候,直接通过下标值就可以获取到对应的元素,时间复杂度为O( ...
- 基于Hi3559AV100 RFCN实现细节解析-(2)RFCN数据流分析
下面随笔系列将对Hi3559AV100 RFCN实现细节进行解析,整个过程涉及到VI.VDEC.VPSS.VGS.VO.NNIE,其中涉及的内容,大家可以参考之前我写的博客: Hi3559AV100的 ...
- 翻译:《实用的Python编程》03_04_Modules
目录 | 上一节 (3.3 错误检查) | 下一节 (3.5 主模块) 3.4 模块 本节介绍模块的概念以及如何使用跨多个文件的函数. 模块和导入 任何一个 Python 源文件都是一个模块. # f ...
- mock 请求分发
首发于 语雀文档 背景是这样的 我们公司的后管项目走的不是 resful 风格的 api,而是走后管网关,后管网关会将请求进行分发,具体怎么分发,有这么以下几点: 请求全部走 POST 请求 URL ...
- mysql基本指令2
pymysql: - 连接.关闭(游标) - execute() -- SQL注入 sss' or 1=1 -- - 增删改: conn.commit() - fetchone f ...