构造函数和析构函数

本文详细介绍了C++中类的封装原理,包括无参和有参构造函数、拷贝构造函数、类型转换构造函数以及析构函数的作用。通过实例演示了如何创建对象并理解this指针的重要性。特别关注了默认构造函数的声明与隐式类型转换的潜在风险。
摘要由CSDN通过智能技术生成

c++封装:类和对象实现封装;
作用:提高代码的维护性,保证代码的独立性(高内聚低耦合)

编译器调用时会加上变量的地址

定义一个对象就会生成一个this指针:指向当前操作的对象

为什么需要this指针:因为同一个类实例的对象共享代码段(方法),为了区分访问哪个对象的成员,所以编译器会在编译阶段给类的所有方法加上this指针参数,用来指向当前操作的对象;

构造函数

构造函数:用来初始化对象的成员的值
引入的作用:防止忘记初始化
特点:
1、函数名与类名相同
2、没有返回值
3、可以重载
4、自动调用
构造函数的种类:
1、默认的无参构造函数
2、有参构造函数
3、拷贝构造函数
4、类型转换构造函数
5、移动拷贝构造函数

无参构造函数:调用无参构造函数:默认的无参构造函数(类里没有任何构造函数时,系统会默认生成)Test t;
Test() = default; 声明无参构造函数使用系统默认生成的;
Test() = delete;不让系统默认生成无参构造函数

#include<iostream>
using namespace std;

class Test
{
public:
	Test()
	{
		this->name = nullptr;
		cout << "Test" << endl;
	}
	//Test() = default; //声明无参构造函数使用系统默认生成的;
	//Test() = delete;//不让系统默认生成无参构造函数
	int num;
	char* name;
	int age;

};
int main()
{
	Test t;

	return 0;
}

有参构造函数:可以重载

#include<iostream>
using namespace std;

class Test
{
public:
	Test(int num,char *name ,int age)
	{
		this->num = num;
		this->age = age;
		//this->name=name;//error
		int len = strlen(name);
		this->name = new char[len + 1];
		strcpy(this->name, name);
		
		cout << "Test int char* int" << endl;
	}
	Test(int num, int age)
    {
        this->num = num;
        this->age = age;
        this->name = nullptr;
        cout << "Test int int" << endl;
    }

	
	int num;
	char* name;
	int age;

};
int main()
{
	Test t1(1, "zhangsan", 21);//有参构造函数
    Test t2(2,23);
	return 0;
}

拷贝构造函数 : 用已有的对象初始化新的对象
默认拷贝构造函数(问题:浅拷贝问题)
自定义拷贝构造函数(深拷贝)
拷贝构造函数的调用时机:
1、用已有的对象初始化新的对象
2、传参:当形参为类的对象时,传参时会调用拷贝构造函数
3、函数返回值为对象时;

#include<iostream>
using namespace std;

class Test
{
public:
	Test(int num,char *name ,int age)
	{
		this->num = num;
		this->age = age;
		//this->name=name;//error
		int len = strlen(name);
		this->name = new char[len + 1];
		strcpy(this->name, name);
		
		cout << "Test int char* int" << endl;
	}

	Test(const Test& other)
	{
		this->age = other.age;
		this->num = other.num;
		int len = strlen(other.name);
		this->name = new char[len + 1];
		strcpy(this-> name, other.name);

        cout << "Test copy Test" << endl;
	}
	void test1(Test t)
    {
        cout << t.name << endl;
    }

    Test test2()
    {
        Test temp(1, "lisi", 24);
        return temp;
    }
	int num;
	char* name;
	int age;

};
int main()
{
	Test t1(1, "zhangsan", 21);
	Test t3(t1);//用已有的对象初始化新的对象
	cout << t3.name << "" << t3.num << endl;
    
    test1(t1);//传参:当形参为类的对象时,传参时会调用拷贝构造函数 

    Test t2 = test2();//函数返回值为对象时;
    
	return 0;
}

类型转换构造函数:构造函数只有一个参数的,都是类型转换构造函数;
用explicit防止发生类型转换

//定义在class里
 explicit Test(int num)  //防止发生隐式转换
    {
        this->num = 0;
        this->age = 0;
        this->name = nullptr;
        cout << "Test int" << endl;
    }

    explicit Test(const char* name)
    {
        this->name = nullptr;
        this->num = 0;
        this->age = 0;
    }
//下面为main函数里的内容
 //Test t4 = 1; //调用类型转换构造函数! 危险(隐式类型转换:其他可以转换成类类型)
    Test t5(1);

    //Test t6 = "hello world";
    Test t7;
    //t7 = 1;

移动拷贝构造函数: 解决临时对象拷贝效率问题

//定义在class里
 Test(Test&& other)//移动拷贝拷贝构造函数
    {
        this->num = other.num;
        this->age = other.age;
        this->name = other.name;
        other.name = nullptr;

        cout << "Test copy Test &&" << endl;
    }
    //在main里
    Test(t1);

析构函数

析构函数:释放对象成员所分配的内存空间
引入的目的:防止内存泄露
特点:
1、函数名是 ~类名
2、无参
3、不可重载
4、没有返回值
5、当对象被释放,会先调用析构函数

//在class里
~Test()
    {
        if (name != nullptr)
        {
            delete[] name;
        }

        cout << "~Test" << endl;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&*Savior

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值