NDK13_C++基础:构造函数、拷贝构造函数、浅拷贝与深拷贝

NDK开发汇总

一 查看内存:

adb shell 进入,命令: dumpsys meminfo 包名

MyTeacher teacher;
  • C++中 这个语句执行完毕,在当前的堆内存内 初始化并且赋值好该对象
  • 在java中执行这个语句,只是开辟了一块内存空间,并没初始化和赋值对象,
    必须用new关键字,来进行初始化和赋值

二 引用

Teacher.h

class MyTeacher
{
public:
	
	~MyTeacher();//析构函数 释放在构造函数里面动态申请的内存 (free)

	MyTeacher(int age,char *name); 

	void setAge(int age);
	int getAge();
	void setName(char *name);
	char* getName();

private:
	int age;
	char *name;
};

Teacher.cpp

#include <Teacher.h>
#include <iostream>

//c++ 标准库的命名空间
using namespace std;

MyTeacher::MyTeacher(int age,char *name):name(name),age(age) {
	cout << " MyTeacher 构造函数  地址:" << this << endl;
}

MyTeacher::~MyTeacher() {
	cout << " MyTeacher 析构函数 地址:" << this << endl;
}
// ::代表限定符
void MyTeacher::setAge(int age) {
	this->age = age;
}

int MyTeacher::getAge() {
	return this->age;
}

void MyTeacher::setName(char *name) {
	this->name = name;
}

char* MyTeacher::getName() {

	return this->name;
}

调用:

#include <Teacher.h>
#include <iostream>

using namespace std;

void fun() {
	MyTeacher teacher = MyTeacher(18, "zhangsan");

	cout << "teacher name:" << teacher.getName() << endl;
	cout << "teacher age:" << teacher.getAge() << endl;
}

void main() {

	fun();

	system("pause");
}

结果:

 MyTeacher 构造函数  地址:00D7F884
teacher name:zhangsan
teacher age:18
 MyTeacher 析构函数 地址:00D7F884

三 指针

void fun() {

	MyTeacher *teacher = new MyTeacher(18, "zangsan");
	cout << "teacher name:" << teacher->getName() << endl;
	cout << "teacher age:" << teacher->getAge() << endl;
	//	delete teacher;
//	teacher = nullptr;
}

结果:

 MyTeacher 构造函数  地址:0113E950
teacher name:zangsan
teacher age:18

没有调用析构函数,需要delete teacher;才会调用

四 拷贝构造函数

使用场景

  1. 值传递
  2. 赋值语句
    MyStudent.h
class MyStudent
{
public:
	MyStudent(int age,char *name,char *teacherName);
	~MyStudent();
	//重写默认的拷贝构造函数
	MyStudent(const MyStudent &student);
public:
	int age;
	char *name;
	char *teacherName;
};

MyStudent.cpp

#include <MyStudent.h>
#include <iostream>

using namespace std;
MyStudent::MyStudent()
{
	cout << " MyStudent 构造函数  地址:" << this << endl;
}

MyStudent::~MyStudent()
{
	cout << " MyStudent 析构函数  地址:" << this << endl;
}

//默认构造函数 浅拷贝
MyStudent::MyStudent(const MyStudent &student) {
	cout << " MyStudent 拷贝构造函数  地址:" << this << endl;
	this->age = student.age;
	this->name = student.name;
	this->teacherName = student.teacherName;
}

调用:

void setFunX(MyStudent student) {
	cout << "setFunc student name:" << student.name << endl;
	cout << "setFunc student age:" << student.age << endl;
	cout << "setFunc student teacherName:" << student.teacherName << endl;
}
void main() {
	MyStudent  student = MyStudent(21, "jack", "Jone");
	setFunX(student);

	system("pause");
}

结果:

 MyStudent 构造函数  地址:008FFE74
 MyStudent 拷贝构造函数  地址:008FFD88
setFunc student name:jack
setFunc student age:21
setFunc student teacherName:Jone
 MyStudent 析构函数  地址:008FFD88

五 浅拷贝与深拷贝

  • 默认的拷贝构造函数是浅拷贝
    浅拷贝:
void copyTest(){
 MyStudent  student = MyStudent(21, "jack", "Jone");
 MyStudent st2 = student;
 }

在这里插入图片描述
浅拷贝出现的问题
修改构造、析构函数

MyStudent::MyStudent(int age, char *name, char *teacherName):age(age)
{
	cout << " MyStudent 构造函数  地址:" << this << endl;
	int len = strlen(name);
	this->name = (char *)malloc(len + 1);

	strcpy(this->name, name);

	len = strlen(teacherName);
	this->teacherName = (char *)malloc(len + 1);
	strcpy(this->teacherName, teacherName);
}

MyStudent::~MyStudent()
{
	cout << " MyStudent 析构函数  地址:" << this << endl;
	free(this->name);
	free(this->teacherName);
}

错误:

C4996	'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	Project2	c:\users\pf0zybaj\source\repos\project2\project2\src\mystudent.cpp	11

解决:属性 ->C/C+±>命令行 :-D _CRT_SECURE_NO_WARNINGS ->应用

继续运行,打印正常,任意键退出时对报错,free已经释放了,拷贝构造函数执行了,拷贝对象释放野指针释放出错

为了避免浅拷贝出现的问题有两个解决方法:
1 深拷贝

MyStudent::MyStudent(const MyStudent &student) {
	cout<<"MyStudent 深拷贝构造函数 地址:" << this << endl;
	int len = strlen(student.name);
	this->name = (char *)malloc(len + 1);

	strcpy(this->name, student.name);

	len = strlen(student.teacherName);
	this->teacherName = (char *)malloc(len + 1);
	strcpy(this->teacherName, student.teacherName);
}

2 私有化拷贝构造函数(不常用)

六 Demo

NDK13_C++基础:构造函数、拷贝构造函数、浅拷贝

发布了269 篇原创文章 · 获赞 123 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/baopengjian/article/details/105101785