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、析构函数、自动类型转换的更多相关文章

  1. C++程序设计方法3:禁止自动类型转换

    禁止自动类型转换 explicit #include <iostream> using namespace std; class Src;//前置类型声明,因为在Dst中要用到Src的类 ...

  2. JavaScript系列文章:自动类型转换-续

    在上一篇文章中,我们详细讲解了JavaScript中的自动类型转换,由于篇幅限制,没能覆盖到所有的转换规则,这次准备详细讲解一下. 上次我们提到了对象类型参与运算时转换规则: 1). 在逻辑环境中执行 ...

  3. JavaScript系列文章:自动类型转换

    我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...

  4. struts基于ognl的自动类型转换需要注意的地方

    好吧,坎坷的过程我就不说了,直接上结论: 在struts2中使用基于ognl的自动类型转换时,Action中的对象属性必须同时添加get/set方法. 例如: 客户端表单: <s:form ac ...

  5. 慕课网-安卓工程师初养成-2-9 Java中的自动类型转换

    来源:http://www.imooc.com/code/1236 在 Java 程序中,不同的基本数据类型的数据之间经常需要进行相互转换.例如: , 代码中 int 型变量 score1 可以直接为 ...

  6. 【转】JavaScript系列文章:自动类型转换

    我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...

  7. c语言的自动类型转换

    转自c语言的自动类型转换 自动转换遵循以下规则: 1)        若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2)        转换按数据长度增加的方向进行,以保证精度不降低.如 ...

  8. C语言自动类型转换

    自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.(eg:int型和long型运算时,先把int量转成lo ...

  9. python小白——进阶之路——day2天-———变量的缓存机制+自动类型转换

    # ###同一文件,变量的缓存机制 ''' -->Number 部分 1.对于整型而言,-5~正无穷范围内的相同值 id一致 2.对于浮点数而言,非负数范围内的相同值 id一致 3.布尔值而言, ...

  10. js 自动类型转换

    js自动类型转换 1.==符号在判断左右两边数据是否相等时,如果数据类型一致,直接比较值即可 2.==符号左右数据类型不一致时,如果两方数据为布尔型.数值型.字符串型时,不为数字的那一方自动调用Num ...

随机推荐

  1. Hyperf-JsonRpc使用

    Hyperf-JsonRpc使用 标签(空格分隔): php 安装扩展包 composer require hyperf/json-rpc composer require hyperf/rpc-se ...

  2. Vue框架简介及简单使用

    目录 一.前端框架介绍 二.vue框架简介 三.vue使用初体验 1. vue如何在页面中引入 2. 插值表达式 3. 文本指令 4. 方法指令(事件指令) 5. 属性指令 四.js数据类型补充 1. ...

  3. 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组

    剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...

  4. HDOJ-1213(简单并查集)

    How many tables HDOJ-1213 #include<iostream> #include<cstring> #include<cstdio> #i ...

  5. dom_bom学习

    1. bom是什么? browser object model(浏览器对象模型) 在浏览器中 window指的就是bom对象 //网页重定向 // window.location.href=" ...

  6. Go ORM框架 - GORM 踩坑指南

    今天聊聊目前业界使用比较多的 ORM 框架:GORM.GORM 相关的文档原作者已经写得非常的详细,具体可以看这里,这一篇主要做一些 GORM 使用过程中关键功能的介绍,GORM 约定的一些配置信息说 ...

  7. css实现0.5像素的底边框。

    由于设计图的1px在移动端开发中的像素比是2倍,在实际开发中却是需要1px的线条,虽然最直接的方式是将线条设置为0.5px,但有些移动端对于0.5px的解析为0,变成了无边框的显示.因此处理该需求我们 ...

  8. 二叉树的建立与遍历(c语言)入门

    树其实在本质上就是一对多,链表就是一对一. 二叉树的建立: 这里的代码采用的是最粗暴的创建方法,无实际用处.但初次学习二叉树可以通过这个创建方法更好的理解二叉树. 二叉树的遍历: 遍历在大体上分为递归 ...

  9. C# 自定义时间进度条

    这篇文章对我帮助极大,我模仿着写了两遍大概摸清楚了自定义控件的流程.https://www.cnblogs.com/lesliexin/p/13265707.html 感谢大佬 leslie_xin ...

  10. MIT 6.824拾遗(一)聊聊basic-paxos

    前言 The Paxos algorithm, when presented in plain English, is very simple. ------ Lamport,<Paxos Ma ...