类的普通构造函数: 函数名与类名一样的函数;
拷贝构造函数:函数名与类名一样 参数也是类的类型;
析构函数:~String(void) delete 删除构造函数;
赋值构造函数:重载运算符= 关键字(operator)
//析构函数是默认最后执行的,与它所存在的位置无关
在内存管理中,都是指针(静态)存在栈里面,申请的空间(非静态)存在堆中;
内存分配方式有三种:
(1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的
整个运行期间都存在。例如全局变量,static 变量。
(2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函
数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集
中,效率很高,但是分配的内存容量有限。
(3) 从堆上分配,亦称动态内存分配。程序在运行的时候用 malloc 或 new 申请任意
多少的内存,程序员自己负责在何时用 free 或 delete 释放内存。动态内存的生存
期由我们决定,使用非常灵活,但问题也最多。
int a={1,2,3};
int *p;
p=a;
free:释放后还会存在首地址的储存p=a;释放的不够干净;
delete:释放后会p=0;释放的更加干净
#pragma once
#include <iostream>
class String
{
public:
String(); //默认构造函数
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~String(void); // 析构函数
String & operator = (const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
#include "Microsoft.h"
// String 的析构函数
String::~String(void)
{
delete[] m_data;
//由于 m_data 是内部数据类型,也可以写成 delete m_data;
}
// String 的普通构造函数
String::String(const char *str)
{
if (str == NULL)
{
m_data = new char[1]; //若能加 NULL判断则更好;
*m_data = '\0';
}
else
{
int lenth = strlen(str);
m_data = new char[lenth+1]; //若能加 NULL 判断则更好
strcpy(m_data,str);
}
}
// 拷贝构造函数
String::String(const String &other)
{
int length = strlen(other.m_data);
m_data = new char[length+1]; //若能加 NULL 判断则更好
strcpy(m_data, other.m_data);
}
// 赋值函数
String & String::operator = (const String &other)
{
// 1. 检查自赋值;
if (this == &other)
{
return *this;
}
// 2. 释放原有的内存资源;
delete[] m_data;
// 3. 分配新的内存资源, 并复制内容;
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
// 4. 返回本对象的引用;
return *this;
}