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 ...
随机推荐
- Dubbo之高级特性
Dubbo 注意当启动服务时,该服务会占用本机一个端口号,故在一台电脑启动多个服务时需要在配置文件中更占用本机的端口号 <!--服务占用本机的端口-当本机启动多个服务时须保持不同--> & ...
- RabbitMQ(二) Java使用RabbitMQ
2-1 RabbitMQ 生产者消息发送 创建 Maven 项目 Send 加入依赖 <dependency> <groupId>com.rabbitmq</groupI ...
- Python3+pygame实现的俄罗斯方块 代码完整 有演示效果
一.简单说明 80.90后的小伙伴都玩过"俄罗斯方块",那种"叱咤风云"场景 偶尔闪现在脑海 真的是太爽了:如果没有来得及玩过的同学,这次可以真正的自己做一个了 ...
- PAT-1043(Is It a Binary Search Tree)JAVA实现
Is It a Binary Search Tree PAT-1043 主要涉及到根据前序遍历序列片段是否是一颗二叉树,这里有一个小tip就是插入序列就是二叉树的前序遍历序列. 第二个是会对排序二叉树 ...
- Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
题目链接:http://codeforces.com/gym/100923/problem/L 分析:题目要求序列首尾相同,在此基础上的字典序第k个:因为只存在a,b所以我们把它等效成0和1的话,字典 ...
- Git 常用命令 和 安装
这年头不会点git还真不能与别人进行代码交流 安装 windowns版下载:https://git-scm.com/download/win ,下载完成后就自己手动安装 ,很简单就不多说. Ubunt ...
- C++树——遍历二叉树
在讲遍历之前,我们要先创建一个树: #include <iostream> using namespace std; typedef struct node; typedef node * ...
- JSP, EL, JSTL的使用
JSP基础指令和语法 回顾 在Jsp页面: 只要是Java代码就会原封不动的输出, 如果是html代码,就会转义为 out.write("<html>\r\n") 这样 ...
- External Libraries中没有Maven的jar包的原因(已解决)
**深坑!** ## External Libraries中没有Maven的jar包的原因(已解决) 2021年3月1日 --- 搭建一个新项目 IDEA 从 Git 上拉 拉去Maven项目然后 m ...
- Python接口自动化实现
一.代码结构: 二.接口签名实现: 1. 设所有发送的数据集合为M,将集合M内非空参数值的参数按照[参数名+"="+参数值]的ASCII码从小到大排序(字典序),然后按拼接key1 ...