【C++】构造函数,this指针学习笔记

class Date
{
public:
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << "." << endl;
	}
//private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	cout << sizeof(Date) << endl;

	Date d1;
	Date d2;
	d1.Print();
	d2.Print();
	d1._year = 1;
	d2._year = 2;
	return 0;
}

class a
{
public:
	void fa() {}
};

int main()
{
	cout << sizeof(a) << endl;
	//算出来是1,分配1byte,占位,不存储数据,要表示你存在
	return 0;
}

 this指针

this在实参和形参位置不能显示写
但是在类里面可以显示的用

class Date
{
public:
	void Init(int year, int month, int day)
	{
		cout << this << endl;

		this->_year = year;
		_day = day;//this省略了
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << "." << endl;
	}
	//private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Init(1, 1, 1);
	return 0;
}

this值可以一样,如果都是d1调用,就是一样,d2调用就和d1不一样
不一定指向同一块空间,看指向的是哪个对象,this只是一个形参,是看实参的

class A
{
public:
	void Print()
	{
		cout << "Print" << endl;
	}
private:
	int _a;
};


int main()
{
	A* p = nullptr;
	(*p).Print();//可以正常运行,看起来解引用,实际没有,print不在对象中,在公共代码区,不需要找print
	//只需要传递this指针,然后call地址
	//主要是编译器会优化
	p->Print();//正常运行,不会解引用,原因同上

	return 0;
}

class B
{
public:
	void Print()
	{
		cout << _a << endl;
	}
private:
	int _a;
};

int main()
{
	A* p = nullptr;
	p->Print();//运行崩溃,传过去this是空的
	return 0;
}

this不能修改,但指向的内容可以修改
封装即权限
this不存在对象里面,他是一个形参,一般是存在栈帧里面的
vs下面一般会用ecx寄存器直接传递

构造函数
构造函数是特殊的成员函数,他的主要任务并不是开空间创建对象,而是初始化对象
特性:
函数名与类型名相同
无返回值(不需要写void)
对象实例化时编译器自动调用对应的构造函数
构造函数可以重载(本质就是我们可以写多个构造函数,提供多种初始化方式)

class Date
{
public:
	//Date()
	//{
	//	_year = 1;
	//	_month = 1;
	//	_day = 1;
	//}
	//无参和全缺省语法上可以存在,但是编译会报错,有二义性,不知道用哪个
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}//可以合并

	//void Init(int year, int month, int day)
	//{
	//	cout << this << endl;

	//	this->_year = year;
	//	_month = month;
	//	_day = day;//this省略了
	//}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << "." << endl;
	}
	//private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

int main()
{
	Date d1;
	//Date d1();error 会认为对象没有调用出来
	//编译器不允许,会分不清楚这是要定义一个对象还是一个函数的声明
	d1.Print();//调用第一个date
	Date d2(2023, 7, 20);
	d2.Print();//调用第二个date
	Date d3(2023, 6);
	d3.Print();
	return 0;
}

    构造函数,也是默认成员函数,我们不写,编译器也会自动生成
    编译生成的默认构造的特点:
    我们不写才会生成,我们写了就不会生成了
    内置类型的成员不会处理(c++11,声明支持给缺省值),自定义类型的成员才会处理
    会去调用这个成员的构造函数
    一般情况都需要我们自己写构造函数,决定初始化方式
    成员变量全是自定义类型,可以考虑不写构造函数

    只要是指针,都是内置类型
    默认构造函数只能有一个,多个会存在二义性

析构函数

析构函数完成对象中资源的清理工作
特性
析构函数名是在类名前加上~
无参数无返回值类型
会自动调用

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}//可以合并

	//void Init(int year, int month, int day)
	//{
	//	cout << this << endl;

	//	this->_year = year;
	//	_month = month;
	//	_day = day;//this省略了
	//}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << "." << endl;
	}
	~Date()
	{
		cout << "~Date" << endl;
	}
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值