0
点赞
收藏
分享

微信扫一扫

C++中的结构体——结构体案例1_2

海滨公园 2024-06-13 阅读 7

文章目录

  • 前言
  • 一.对象的动态建立和释放
  • 二.多个对象的构造和析构
  • 三.深拷贝与浅拷贝
  • 四.C++类的内存管理
  • 总结


前言 

我们前面讲起了一些关于C++中类与对象的一些语法,构造函数C构函数初始化成员列表等等,也讲了面对对象的程序设计方法和面对过程的程序设计方法有什么区别,我们这次就主要针对类与对象的拷贝和一些存储内存的角度继续了解


一.对象的动态建立和释放

格式:
new
1、类型指针 指针变量名 = new 类型
2、类型指针 指针变量名 = new 类型(初始值)
3、类型指针 指针变量名 = new 类型[元素个数]
delete
1、delete 指针变量名
2、delete[] 指针变量名

int main()
{
	//在堆上申请一个int类型大小的空间,并且将申请的空间初始化为10
	int* p1 = new int(10);
	delete p1;
	//在堆上申请4个int类型大小的空间,并没初始化
	int* p2 = new int[4];
	delete[4] p2;
	//在堆上申请一个Box类型大小的空间,会构造对象出来
	Box* p3 = new Box;
	delete p3;
	//在堆上申请4个Box类型大小的空间,会构造对象出来
	Box* p4 = new Box[4];
	delete[4] p4;
	return 0;
}

注意

new和delete是运算符,不是函数,因此执行效率高。虽然为了与C语言兼容,C++仍保留malloc和free函数,但建议用户不用malloc和free函数,而用new和delete运算符。new/delete 和 malloc/free有何取别呢?

二.多个对象的构造和析构

注意:1.当类中的成员变量为另一个类的实例化对象时,我们称这个对象为成员对象。2.成员变量虽属的类中没有实现无参的构造函数是需要使用初始化成员列表。

#include<iostream>
using namespace std;
class ABC
{
public:
	ABC(int A, int B, int C)
	{
		cout << "ABC(int A, int B, int C)" << endl;
	}
	~ABC()
	{
		cout << "~ABC()" << endl;
	}
private:
	int a;
	int b;
	int c;
};
class myD
{
public:
	myD() :abc1(1, 2, 3), abc2(3, 5, 7)
	{
		cout << "myD()" << endl;
	}
	~myD()
	{
		cout << "~myD()" << endl;
	}


private:
	ABC abc1;
	ABC abc2;
};
int main()
{
	myD a;

	return 0;
}

调用顺序

三.深拷贝与浅拷贝

3.1拷贝构造函数

//拷贝构造函数
Test(const Test& t)
{
	cout << "Test(const Test& t)" << endl;
}

3.2对象的赋值

思考这样的赋值对吗?

#include<iostream>
using namespace std;
class Test
{
public:
	int x;
	int y;
	int* sum;
	Test(int a, int b):x(a),y(b)
	{
		sum = new int[4];
	}
	//拷贝构造函数
	Test(const Test& t)
	{
		cout << "Test(const Test& t)" << endl;
		x = t.x;
		y = t.y;
		sum = t.sum;
	}
	~Test()
	{
		delete[4] sum;
	}
};
int main()
{
	Test t1(10,20);
	t1.sum[0] = 10; t1.sum[1] = 11; t1.sum[2] = 12; t1.sum[3] = 13;
	Test t2 = t1;
}

class Test
{
public:
	int x;
	int y;
	int* sum;
	Test(int a, int b):x(a),y(b)
	{
		sum = new int[4];
	}
	//拷贝构造函数
	Test(const Test& t)
	{
		cout << "Test(const Test& t)" << endl;
		x = t.x;
		y = t.y;
		//浅拷贝
	    //sum = t.sum;
		//深拷贝
		sum = new int[4];
	for (int i = 0; i < 4; i++)
	{
		sum[i] = t.sum[i];
	}
	}
	~Test()
	{
		delete[4] sum;
	}
};

3.3浅拷贝

3.4深拷贝

四.C++类的内存管理

证明:

#include<iostream>
using namespace std;
class C1
{
public:
	int i;
	int j;
	int k;
};
class C2
{
public:
	int i;
	int j;
	int k;
	int getK()
	{
		return k;
	}
	void setK(int val)
	{
		k = val;
	}
};

int main()
{
	C1 c1;
	C2 c2;
	cout << sizeof(c1) << endl;
	cout << sizeof(c2) << endl;
}

4.2this指针

using namespace std;
class ABC
{
public:
	int x, y, z;
	ABC(int x, int y, int z)
	{
		x = x;
		y = y;
		z = z;
	}
};
int main()
{
	ABC a(1, 2, 3);
	return 0;
}

经过编译

class ABC
{
public:
	int x, y, z;
	ABC(ABC*const this,int x, int y, int z)
	{
		this->x = x;
		this->y = y;
		this->z = z;
	}
};
int main()
{
	//&a就是this指针
	ABC a(&a,1, 2, 3);
	return 0;
}

4.3类的静态成员变量

#include<iostream>
using namespace std;
class sheep
{
public:
	int age;
	char name[32];
	sheep()
	{
		cnt++;
	}
	~sheep()
	{
		cnt--;
	}
	static int cnt;
};

int sheep::cnt = 0;
int main()
{

	return 0;
}

4.4类的静态成员函数

定义:使用static修饰的成员函数叫做静态成员函数

什么时候可以将函数设计成静态成员函数?

静态成员函数的用处:


总结

这次我们主要讲解了对象的动态开辟和释放对比C语言的不同,和前面所讲到的析构和构造的一个升华,是多对象的析构和构造,还讲了C++独特的浅拷贝和深拷贝以及C++类的一些内存管理如类的静态成员变量静态成员函数和this指针

举报

相关推荐

结构体案例2

C++ 结构体

C++: 结构体

C++结构体

C++ 结构体 struct

0 条评论