C++笔记-20-对象特性

对象模型和this指针

成员变量和成员函数分开存储

在C++中,类内的成员变量和成员函数分开存储

只有非静态成员变量才属于类的对象上

cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
class Person {
	int m_A;
	static int m_B;
	void func() {
		cout << "666" << endl;
	}
	static void func2() {
		cout << "666" << endl;
	}
};
int Person::m_B;
int main()
{
	Person p;
	cout << sizeof(p) << endl;
	return 0;
}

Person如果是空的,会占用一个字节内存区分其他内存地址,如果不为空,例如有一个int的m_A,那么占用的内存大小为4个字节,也就是m_A的大小。然后成员函数和静态变量,都不是储存在类的对象上的。所以哪怕加了一个静态成员变量,两个函数,p的大小依然是4个字节。

this指针

this指针是隐含每一个非静态成员函数内的一种指针

this指针不需要定义,直接使用即可

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this;
cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
class Person {
public:
	int age;
	Person(int age) {
		this->age = age;
	}
	Person& PersonAddPerson(Person& p) {
		this->age += p.age;
		return *this;
	}

};
int main()
{
	Person p1(10);
	Person p2(10);
	p2.PersonAddPerson(p1).PersonAddPerson(p1);
	cout << p2.age << endl;

}

其中PersonAddPerson(Person& p)函数,返回值必须是Person &,如果只是Person,那么接受一个*this的解引用,相当于是通过拷贝构造函数创造了一个新的Person变量,那么下面的链式调用就不会生效了。

空指针访问成员函数】

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
class Person {
public:
	void showClassName() {
		cout << "this is person" << endl;
	}
	int age;
	void showPersonAge() {
		if (this == NULL) {
			return;
		}
		//会报错,传入的指针为空,也就是this指针会为空。
		//加入一个判断可以防止程序崩溃。
		cout << age << endl;
	}
};
int main()
{
	Person* p = NULL;
	p->showClassName();
	p->showPersonAge();
}

const修饰成员

常函数:

  • 成员函数后加const我们称为这个函数为常函数
  • 常函数内不可以修改成员属性
  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数
cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
class Person {
public:
	void showPerson() const {
		a = 100;
	}
	void showinfo() {
		cout << "this is a function" << endl;
	}
	int age;
	mutable int a;
};
int main()
{
	const Person p;
	//p.age = 100;常对象不能修改成员变量
	p.a = 100;
	p.showPerson();
	//p.showinfo();常对象不能调用普通函数
}

this指针其实是指针常量,int * const this*,只能修改指针的值,不能修改指针的指向。常函数其实是让this指针变为了const int * const this`,也就导致了成员变量不能修改。如果需要修改只需要在成员变量前加上mutable关键字。

友元

全局函数做友元

友元的目的就是让一个函数或者类访问另一个类中私有成员

友元的关键字为friend

cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
#include<cmath>
#include<string>
class Building {
	//声明goodfriend是building的好朋友
	friend void goodfriend(Building& bd);
public:
	string m_sr;
	Building() {
		m_sr = "客厅";
		m_br = "卧室";
	}
private:
	string m_br;
};
void goodfriend(Building& bd)
{
	cout << bd.m_sr << endl;
	cout << bd.m_br << endl;
}
int main()
{
	Building bd;
	goodfriend(bd);
	return 0;
}

全局函数做友元只需要在类内写上函数的声明,然后在前面加上friend关键字即可。

类做友元

cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
#include<cmath>
#include<string>
class Building {
	friend class Goodfriend;
public:
	string m_sr;
	Building() {
		m_sr = "客厅";
		m_br = "卧室";
	}
private:
	string m_br;
};

class Goodfriend {
public:
	Building* building;
	Goodfriend() {
		building = new Building;
	}
	void visit() {
		cout << building->m_sr << endl;;
		cout << building->m_br << endl;;
	}

};
int main()
{
	Goodfriend gf;
	gf.visit();
	return 0;
}

只需要把类的声明放在另外一个类内,在前面加上friend关键字即可。

成员函数做友元

cpp 复制代码
#include<iostream>
using namespace std;
#include<iomanip>
#include<cmath>
#include<string>
class Building;
class Goodfriend {
public:
	Goodfriend();
	Building* building;
	//让visit1可以访问Building的私有成员
	void visit1();
	//让visit2不可以访问Building的私有成员
	void visit2();
};
class Building {
	friend void Goodfriend::visit1();
public:
	Building();
	string m_sr;
private:
	string m_br;
};
Building::Building() {
	m_sr = "客厅";
	m_br = "卧室";
}
Goodfriend::Goodfriend() {
	building = new Building;
}
void Goodfriend::visit1() {
	cout << building->m_sr << endl;
	cout << building->m_br << endl;
}
void Goodfriend::visit2() {
	cout << building->m_sr << endl;
	//cout << building->m_br << endl;不可访问
}
int main()
{
	Goodfriend gf;
	gf.visit1();
	gf.visit2();
	return 0;
}

成员函数做友元和全局函数做友元基本一样,但是要在函数名前加上作用域,指明是哪个类里面的成员函数。

相关推荐
2zcode25 分钟前
基于MATLAB深度学习的乳腺钼靶影像乳腺癌智能诊断系统设计与实现
开发语言·深度学习·matlab·乳腺癌
MoonBit月兔29 分钟前
错过大赛报名?MoonBit 开源大赛- 8 月黑客松正式开启
开发语言·ai-native·moonbit
fqbqrr41 分钟前
2607d,C++细节比不上d
c++·d
^yi42 分钟前
【C++】继承的那些事儿~
c++·继承·菱形继承·隐藏·final·继承原理·多继承的指针偏移
weixin_538601971 小时前
智能体测开Day31
开发语言·python
geovindu1 小时前
java: Facade Pattern
java·开发语言·后端·外观模式·结构型模式
listening7771 小时前
HarmonyOS 6.1 元服务深度优化:从“秒开”到“常驻”的极致体验
java·开发语言·spring
布朗克1681 小时前
Go 入门到精通-29-测试与基准
开发语言·后端·golang·测试与基准
格林威2 小时前
工业相机Chunk功能全解析:图像嵌入时间戳、编码器元数据(附堡盟C#代码)
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
小大宇2 小时前
Python 集成 Conda
开发语言·python·conda