析构函数和拷贝构造函数的调用顺序

就是类似于栈一样
也可以理解为一个园,

先建立的对象先用拷贝构造函数,后建立的后用,调用析构函数优先,当删除对象时会自动调用析构函数,先删除后建立的,再删除先调用的。
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;

class student {
    
    
	private:
		int year;
		int month;
		int day;
	public:
		student(int y, int m, int d): year(y), month(m), day(d) {
    
    
			cout << year << "." << month << "." << day << endl;
			cout << "调用和拷贝构造函数" << endl;
		}
		~student() {
    
    
			cout << year << "." << month << "." << day << endl;
			cout << "调用析构函数" << endl;
		}
};

int main () {
    
    
	student Fg(2021, 4, 22);
	student Wg(2020, 1, 29);
	Fg.~student();//主动调用的优先调用。
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_52045928/article/details/116052547