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;
}

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

相关推荐
码匠许师傅4 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
2603_965148114 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
bitbrowser4 小时前
giffgaff突然停用?先分清闲置停用和长期境外断开
笔记
九硕智慧建筑一体化厂家5 小时前
数字化管控落地,直流照明构筑安全照明体系
运维·人工智能·笔记·安全·智慧城市
AI云海5 小时前
天幕红尘笔记
笔记
小小龙学IT6 小时前
DuckDB 深度实战:用 C++ 在进程内跑一个「分析型数据库」
数据库·c++
知无不研7 小时前
lambda表达式的使用(3)
开发语言·c++·lambda
cfm_29147 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
青山是哪个青山7 小时前
LangChain 学习笔记(三):LangSmith 调试与监控
笔记·学习·langchain
C++ 老炮儿的技术栈9 小时前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐