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

NDK开发汇总

一 类

C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心特性,用户定义的类型。

class Student {
	int i;    //默认 private
public:
	Student(int i,int j,int k):i(i),j(j),k(k){};	//构造方法 
	~Student(){};	//析构方法 
private:
	int j;
protected:
	int k;
};

Student student(1,2,3); //调用构造方法 栈
//出方法释放student 调用析构方法

//动态内存(堆)
Student *student = new Student(1,2,3);
//释放
delete student;
student = 0;

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行(不需要手动调用)。

private:可以被该类中的函数、友元函数访问。 不能被任何其他访问,该类的对象也不能访问。

protected:可以被该类中的函数、子类的函数、友元函数访问。 但不能被该类的对象访问。

public:可以被该类中的函数、子类的函数、友元函数访问,也可以被该类的对象访问。

查看内存:

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 
#include 

//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 
#include 

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 
#include 

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;
 }

NDK13_C++基础:类、构造函数、拷贝构造函数、浅拷贝与深拷贝_第1张图片
浅拷贝出现的问题
修改构造、析构函数

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++基础:构造函数、拷贝构造函数、浅拷贝

你可能感兴趣的:(NDK)