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

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

相关推荐
tobebetter95273 小时前
How to manage python versions on windows
开发语言·windows·python
FFF团团员9094 小时前
树莓派学习笔记3:LED和Button
笔记·学习
9***P3344 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger4 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
碧海潮生_CC5 小时前
【CUDA笔记】04 CUDA 归约, 原子操作,Warp 交换
笔记·cuda
jllllyuz5 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*5 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
凌康ACG5 小时前
Sciter之c++与前端交互(五)
c++·sciter
摇滚侠5 小时前
2025最新 SpringCloud 教程,从单体到集群架构,笔记02
笔记·spring cloud·架构
p***43485 小时前
Rust网络编程模型
开发语言·网络·rust