简简单单学习Cpp-构造函数的调用规则

简简单单学习Cpp-构造函数的调用规则

规则

  • 如果提供了有参构造 那么编译器不会提供默认的构造函数
  • 如果提供了拷贝构造函数 那么编译器不会提供默认的构造函数和默认的拷贝构造函数
# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "test.h"
using namespace std;

class Maker {
public:
	//构造函数的作用是初始化成员变量  是编译器去调用的
	Maker() {
		cout << "无参构造函数" << endl;
	}

	Maker(int a) {
		cout << "有参构造函数" << endl;
	}

	Maker(const Maker& maker) {
		cout << "拷贝构造函数" << endl;
	}

	// 析构函数在对象销毁前  编译器调用析构函数
	~Maker() {
		cout << "析构函数" << endl;
	}

private:
	int a;
};

void test() {
	// Maker m;// error
	Maker m(10);
	Maker m2(m);// 调用默认的拷贝构造函数
}


int main() {
	// 封装
	Maker m;
	
	system("pause");
	return EXIT_SUCCESS;
}

多个对象的构造函数和析构函数

首先调用成员对象的构造函数 然后调用自己的构造函数,析构函数的调用反过来

# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "test.h"
using namespace std;



class BMW {
public:
	BMW() {
		cout << "BMW构造" << endl;
	}

	~BMW() {
		cout << "BMW的析构函数" << endl;
	}
};


class Buick {
public:
	Buick() {
		cout << "Buick构造" << endl;
	}

	~Buick() {
		cout << "Buick的析构函数" << endl;
	}
};


class Maker {
public:
	//构造函数的作用是初始化成员变量  是编译器去调用的
	Maker() {
		cout << "无参构造函数" << endl;
	}

	Maker(int a) {
		cout << "有参构造函数" << endl;
	}

	Maker(const Maker& maker) {
		cout << "拷贝构造函数" << endl;
	}

	// 析构函数在对象销毁前  编译器调用析构函数
	~Maker() {
		cout << "析构函数" << endl;
	}

private:
	int a;
	BMW b;// 成员对象
	Buick bu;// 成员对象
};


void test() {
	Maker m;
}


int main() {
	// 封装
	test();
	
	system("pause");
	return EXIT_SUCCESS;
}

初始化列表

初始化列表是调用成员对象的指定构造函数
如果使用初始化列表 那么所有的构造函数都要写初始化列表


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "test.h"
using namespace std;



class BMW {
public:
	// 有参构造函数
	BMW(int a) {
		cout << "BMW构造" << endl;
	}

	~BMW() {
		cout << "BMW的析构函数" << endl;
	}
};


class Buick {
public:
	Buick() {
		cout << "Buick构造" << endl;
	}

	~Buick() {
		cout << "Buick的析构函数" << endl;
	}
};


class Maker {
public:
	//构造函数的作用是初始化成员变量  是编译器去调用的
	// 初始化参数列表
	Maker():b(10) {
		cout << "无参构造函数" << endl;
	}

	// 避免写死 直接吧形参进行赋值
	Maker(int a):b(a) {
		cout << "有参构造函数" << endl;
	}

	Maker(const Maker& maker) :b(a) {
		cout << "拷贝构造函数" << endl;
	}

	// 析构函数在对象销毁前  编译器调用析构函数
	~Maker() {
		cout << "析构函数" << endl;
	}

private:
	int a;
	BMW b;// 成员对象
	Buick bu;// 成员对象
};


void test() {
	Maker m;
}

void test2() {

}


int main() {
	// 封装
	test();
	
	system("pause");
	return EXIT_SUCCESS;
}

对象的深拷贝和浅拷贝

默认的拷贝构造函数进行了简单的赋值操作,这个是浅拷贝

浅拷贝案例


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "test.h"
using namespace std;



class Maker {
public:
	Maker(int id, int age) {
		mId = id;
		mAge = age;
	}

	int getId() {
		return mId;
	}

	int getAge() {
		return mAge;
	}
	
private:
	int mId;
	int mAge;
};

void test() {
	Maker m1(1, 11);
	Maker m2(m1);

	cout << "m1 " << m1.getId() << endl;
	cout << "m2 " << m2.getAge() << endl;

}
int main() {
	// 封装
	test();
	
	system("pause");
	return EXIT_SUCCESS;
}

深拷贝
说白了就是在申请一块内存空间,然后把原来的值拷贝进去

class Student {
public:
	Student(const char* name, int Age) {
		pName = (char*)malloc(strlen(name) + 1);
		strcpy(pName, name);
		age = Age;
	}

	// 深拷贝  拷贝构造函数  也就是先申请空间  然后拷贝
	Student(const Student& stu) {
		cout << "自己拷贝的构造函数" << endl;
		// 申请空间
		pName = (char*)malloc(strlen(stu.pName) + 1);
		// 拷贝数据
		strcpy(pName, stu.pName);
		age = stu.age;
	}

	~Student() {
		cout << "析构函数" << endl;

		if (pName != NULL) {

			free(pName);
			pName = NULL;

		}
	}

private:
	char* pName;
	int age;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

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

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

打赏作者

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

抵扣说明:

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

余额充值