c/c++
MagicGeek
这个作者很懒,什么都没留下…
展开
-
C++ delete 和 delete []的区别
转载转载 2020-05-12 17:13:57 · 130 阅读 · 0 评论 -
构造函数,复制构造函数和析构函数
这些函数在网上很容易就可以查到定义和写法,这里就不赘述了。令人感兴趣的是这些函数什么时候执行, 特别是复制构造函数和析构函数。用编写的一段(很丑的)代码来说明。#include<iostream>#include<cstdio>#include<cmath>class Point{private: /* data */ double x, y ; friend Point& __doa(Point *ths, const Point&原创 2020-05-12 15:22:55 · 281 阅读 · 0 评论 -
使用 new 申请动态内存
new 语句比 malloc 语句对程序员更加友好。 写法上,更加简洁, 也更加好懂。以矩阵运算为例。 当我们需要创建一个矩阵的时候, 可以使用二级指针, 在内存中申请一块空间。mat = new int*[row_len] ; for(int i=0 ; i<row_len ; i++){ mat[i] = new int [col_len] ; }观察上面的代码:首先给二级指针mat申请内存。 申请了长度为row_len的内存,类型是一级指针。然后给每个一级指针申请动态内原创 2020-05-11 19:08:00 · 1833 阅读 · 0 评论 -
c++中引用的使用
写矩阵运算的程序的时候,需要用到二级指针。void get_memory(int **&mat){ mat = new int*[row_len] ; for(int i=0 ; i<row_len ; i++){ mat[i] = new int [col_len] ; }}利用 new 语句申请动态内存。其中int **&mat的写法很让人在意。 从字面上看(从右向左看),这应该是“引用的二级指针”。 但是一般的解释说这是二级指针的引用。从意义上来说原创 2020-05-11 19:00:19 · 111 阅读 · 0 评论