new and delete
/*
new 和 delete 的基本使用
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
//C
int * p1 = (int *)malloc(sizeof(int));
*p1 = 10;
//free(p1)
//C++
int *p2 = new int;
*p2 = 10;
//delete p2
int * p3 = new int(10);
//delete p3
cout<<*p1<<*p2<<*p3<<endl;
//C
int * arr1 = (int *)malloc(sizeof(int) * 10);
for (int i = 0;i < 10;i++){
arr1[i] = i + 1;
cout<<"Arr_ONE:"<<arr1[i]<<endl;
}
free(arr1);
//C++
int * arr2 = new int[10];
for (int i = 0;i < 10;i++){
arr2[i] = i + 1;
cout<<"Arr_TWO:"<<arr2[i]<<endl;
}
delete [] arr2;
cout<<"=========================="<<endl;
//C
char ** str1 = (char **)malloc(sizeof(char *) * 5);
for (int i = 0;i < 5;i++){
str1[i] = (char *)malloc(10);
memset(str1[i],0,10);
sprintf(str1[i],"%d%d%d",i+1,i+1,i+1);
cout<<"STR_ONE:"<<str1[i]<<endl;
}
//free
for (int i = 0;i < 5;i++){
if (str1[i] != NULL){
free(str1[i]);
str1[i] = NULL;
}
}
if (str1 != NULL){
free(str1);
str1 = NULL;
}
//C++
char ** str2 = new char* [5];
for (int i = 0;i < 5;i++){
str2[i] = new char[10];
memset(str2[i],0,10);
sprintf(str2[i],"%d%d%d",(i+1),(i+1),(i+1));
cout<<"SRT_TWO:"<<str2[i]<<endl;
}
//delete
for (int i = 0;i < 5;i++){
delete str2[i];
}
delete str2;
cout<<"=========================="<<endl;
//C++ 新增string类型
string * List = new string[5];
for (int i = 0;i < 5;i++){
char * temp = (char *)malloc(10);
sprintf(temp,"%d%d%d",(i+1),(i+1),(i+1));
List[i] = temp;
cout<<"LIST:"<<List[i]<<endl;
free(temp);
}
delete [] List;
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//new-delete :能自动调用(class | struct)的构造函数和析构函数
//malloc-free:不能自动调用(class | struct)的构造函数和析构函数
//C++
using namespace std;
class Test{
public:
Test(){
cout<<"Test()"<<endl;
}
Test(int a){
this->a = a;
cout<<"Test (int)"<<endl;
}
Test(const Test & t){
this->a = t.a;
cout<<"Test (const Test &)"<<endl;
}
~Test(){
cout<<"~Test()"<<endl;
}
private:
int a;
};
//C
struct Teacher{
public:
Teacher(){
cout<<"Teacher()"<<endl;
}
Teacher(int a){
this->a = a;
cout<<"Teacher(int)"<<endl;
}
Teacher ( const Teacher & t){
this->a = t.a;
cout<<"Teacher (const Teacher & )"<<endl;
}
~Teacher(){
cout<<"~Teacher()"<<endl;
}
private:
int a;
};
void runTest(){
// Test * test = new Test;//无参
Test * test = new Test(10);//带参
delete test;
}
void runTeacher(){
// Teacher * teacher = new Teacher;//无参
Teacher * teacher = new Teacher(10);//带参
delete teacher;
}
void run_Test(){
Test * test = (Test *)malloc(sizeof(Test));
free(test);
}
void run_Teacher(){
Teacher * teacher = (Teacher * )malloc(sizeof(Teacher));
free(teacher);
}
int main(int argc, char *argv[])
{
runTest(); //Test(int) -> ~Test()
runTeacher(); // Teacher(int) -> ~Teacher()
run_Test(); //不能自动执行构造函数和析构函数
run_Teacher(); //不能自动执行构造函数和析构函数
return 0;
}
/*
new - delete
malloc - free
混搭
实际编程中并不建议此种做法
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
class Test{
public:
Test(){
cout<<"Test()"<<endl;
}
Test(int a){
cout<<"Test(int)"<<endl;
}
Test(const Test & t){
cout<<"Test(const Test & t)"<<endl;
}
~Test(){
cout<<"~Test()"<<endl;
}
private :
};
int main(int argc, char *argv[])
{
//C/C++混搭
int * p2 = new int;
free(p2);//OK
int * p3 = (int *)malloc(sizeof(int));
delete p3;//OK
//========================
int * arr1 = new int[10];
free(arr1);//OK
int * arr2 = (int *)malloc(sizeof(int) * 10);
delete arr2;//OK
//=========================
char ** list1 = new char* [5];
free(list1);//OK
char ** list2 = (char ** )malloc(sizeof(char *) * 5);
delete list2;//OK
//=========================
/*Error [malloc函数无法正确调用string类的构造函数]
string * str = (string *)malloc(sizeof(string));
*str = "hello world";
cout<<"STR = "<<*str<<endl;
free(str);
*/
string * str2 = new string;
*str2 = "HELLO WORLD";
cout<<"SRT = "<<*str2<<endl;
delete str2;
/*Error [malloc函数无法正确调用string类的构造函数]
string * str3 = (string *)malloc(sizeof(string));
*str3 = "HELLO WORLD";
cout<<"STR = "<<*str3<<endl;
delete str3;
*/
string * str4 = new string;
*str4 = "HEllO WorLD";
cout<<"STR = "<<*str4<<endl;
free(str4);
//==================================
Test * t1 = new Test(10);//OK
free(t1);//Error free不会调用析构函数
Test * t2 = (Test *)malloc(sizeof(Test));//Error malloc不会调用构造函数
delete t2;//OK
return 0;
}
/*
静态成员变量以及静态成员函数
静态成员函数中不允许使用成员变量和成员函数
*/
#include <iostream>
using namespace std;
class Test{
public:
static int NUM;//定义
static void st_show(){
cout<<"NUM = "<<NUM<<endl;
// cout<<"Temp ="<temp<<endl;//Error 普通成员变量
// show_temp();//Error 普通成员函数
}
public:
int temp;
void show_temp(){
cout<<"NUM = "<<NUM<<endl;//OK
st_show();//OK
cout<<"Temp = "<<temp<<endl;
}
};
//静态成员变量须在main函数的外部初始化
int Test::NUM = 1;//初始化
int main(int argc, char *argv[])
{
Test A,B,C;
cout<<"Test::NUM = "<<Test::NUM<<endl;
cout<<"A::NUM = "<<A.NUM<<endl;
B.NUM++;
cout<<"C::NUM = "<<C.NUM<<endl;
//================================
cout<<"=================================="<<endl;
Test::st_show();
A.temp = 10;
A.st_show();
A.show_temp();
return 0;
}
/*
this指针初探
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
using namespace std;
//C++
class Test{
public:
Test(char * name){//隐藏了一个this指针 Test(Test * this,char * name);
this->name = new char[64];
memset(this->name,0,sizeof(this->name));
strcpy(this->name,name);
}
~Test(){//隐藏了一个this指针
delete this->name;
}
public:
void show(){//隐藏了一个this指针
cout<<"Name = "<<this->name<<endl;
}
private:
char * name;
};
//C
typedef struct CTest{
char * name;
}CTest;
void InitTest(CTest * This,char * name){
This->name = (char *)malloc(64);
memset(This->name,0,sizeof(This->name));
strcpy(This->name,name);
}
void DeleteTest(CTest * This){
if (This->name != NULL){
free(This->name);
This->name = NULL;
}
}
void CShow(CTest * This){
printf("Name = %s\n",This->name);
}
int main(int argc, char *argv[])
{
//C++
Test * t = new Test("lcmm");
t->show();
delete t;
//C
CTest * ct = (CTest *)malloc(sizeof(CTest));
InitTest(ct,"lcmm");//在C++中会自动调用
CShow(ct);
DeleteTest(ct);
return 0;
}
一个数组
=====>ARRAY.H
#ifndef ARRAY_H
#define ARRAY_H
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
using namespace std;
class List
{
public:
List(int length);
List(const List & list);
~List();
public:
void setListIndexData(int index,int value);
int getListIndexData(int index);
int getLength();
private:
int length;
int * list;
};
#endif // ARRAY_H
=====>ARRAY.CPP
#include "array.h"
List::List(int length){
this->length = length;
this->list = new int[this->length];
memset(this->list,0,sizeof(int) * this->length);
}
List::List(const List & list){
cout<<"List (const List & )"<<endl;
this->length = list.length;
this->list = new int[this->length];
memcpy(this->list,list.list,this->length * sizeof(int));
}
List::~List(){
delete [] this->list;
}
void List::setListIndexData(int index, int value){
this->list[index] = value;
}
int List::getListIndexData(int index){
return this->list[index];
}
int List::getLength(){
return this->length;
}
=====>MAIN.CPP
/*
* 数组类
*/
#include <iostream>
#include "array.h"
using namespace std;
int main(int argc, char *argv[])
{
/*
List * arrayList = new List(10);
for (int i = 0;i < arrayList->getLength();i++){
arrayList->setListIndexData(i,i+1);
}
for (int i = 0;i < arrayList->getLength();i++){
cout<<"ArrayList["<<i<<"] = "<<arrayList->getListIndexData(i)<<endl;
}
cout<<"====================="<<endl;
List * LinkedList = arrayList; //赋值操作,没有执行拷贝构造函数(ArrayList and LinkedList 共享同一片内存)
for (int i = 0;i < LinkedList->getLength();i++){
cout<<"LinkedList["<<i<<"] = "<<LinkedList->getListIndexData(i)<<endl;
}
delete LinkedList;
delete arrayList;
*/
List arrayList(10);
for (int i = 0;i < arrayList.getLength();i++){
arrayList.setListIndexData(i,i+1);
}
for (int i = 0;i < arrayList.getLength();i++){
cout<<"ArrayList["<<i<<"] = "<<arrayList.getListIndexData(i)<<endl;
}
List linkedList = arrayList; //执行拷贝构造函数
for (int i = 0;i < arrayList.getLength();i++){
cout<<"LinkedList["<<i<<"] = "<<linkedList.getListIndexData(i)<<endl;
}
return 0;
}