day08:类(3)

对象模型和this指针

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

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

class Person {
	int m_A;//非静态成员变量,属于类的对象上的数据
	static int m_B;//静态成员变量,不属于类的对象上的数据
	void func() {};//非静态成员函数,不属于类的对象上的数据
	static void func2() {};//静态成员函数,不属于类的对象上
};
int Person::m_B = 0;
void test01() {
	Person p;
	//空对象占用的内存空间为:1
	//C++编译器会给每个空对象分配一个字节的空间,是为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址
	cout << "size of p = " << sizeof(p) << endl;
}
int main() {
	test01();
}

this指针概念

this指针指向被调用的成员函数所属的对象

this指针是隐含每一个非静态成员函数内的一种指针,同时其不需要定义,可以直接使用

用途:1.当形参和成员变量同名时,可用this指针来区分

2.在类的非静态成员函数中返回对象本身,可用return* this

class Person {
public:
	Person(int age) {//成员函数
		//解决名称冲突
		this->age = age;//this指针指向 被调用的成员函数 所属的 对象,指向p1
	}
	Person(const Person& p) {
		cout << "拷贝构造函数" << endl;
		this->age = age;
	}//值的方式返回,会创建一个新的对象,见拷贝构造函数
	Person& PersonAddAge(Person& p){//引用,会更新p2
		this->age += p.age;//p2更改,可以重复引用同一片空间
		//this指向p2的指针,而*this指向的就是p2这个对象本体
		return *this;
	}
	int age;
};
void test01() {
	Person p1(18);//对象
	cout << "p1's age is " << p1.age << endl;
}
void test02() {
	Person p1(10);
	Person p2(10);
	//链式编程思想 p2.PersonAddAge(p1) = *this = p2
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << p2.age << endl;
}
int main() {
	//test01();
	test02();
}

空指针调用成员函数

空指针访问成员函数可能会引发读取访问权限冲突

class Person {
public:
	void showClassName() {
		cout << "this is a Person class" << endl;
	}
	void showPersonAge() {//报错原因是因为传入的指针为NULL
		if (this == NULL) {//增加判断语句保证程序鲁棒性
			return;
		}
		cout << "age = " << this->m_Age << endl;
	}
	int m_Age;
};
void test01() {
	Person* p = NULL;
	p->showClassName();
	p->showPersonAge();
}
int main() {
	test01();
}

const修饰成员函数

常函数:1.成员函数后加const称为常函数

                2.常函数内不可修改成员属性

                3.成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:1.声明对象前加const称该对象为常对象

                2.常对象只能调用常函数

class Person {
public:
	//this指针的本质是指针常量 指针的指向是不可以修改的
	//this == Person * const this
	//void showPerson() const == const Person * const this,修饰的是this指针,指针指向的值也不可修改
	void showPerson() const{
		//m_A = 100;
		//this = NULL;this指针不可以修改指针指向
		this->m_B = 100;
	}
	void func(){}
	int m_A;
	mutable int m_B;//特殊变量,即使在常函数中,也可以修改
};
void test01() {
	Person p;
	p.showPerson();
}
void test02() {
	const Person p;
	//p.m_A = 100;
	p.m_B = 100;//m_B是特殊值,在常对象中可以修改
	p.showPerson();
	//p.func();//常对象只能调用常函数,因为普通成员函数可以修改属性
}

友元

全局函数做友元

class Building {
	//goodFriend是Building的友元,可以访问Building私有成员
	friend void goodFriend(Building* building);
public:
	Building() {
		m_SittingRoom = "SittingRoom";
		m_Bedroom = "Bedroom";
	}
public:
	string m_SittingRoom;
private:
	string m_Bedroom;
};
void goodFriend(Building* building) {
	cout << "friend is visiting : " << building->m_SittingRoom << endl;
	cout << "friend is visiting : " << building->m_Bedroom << endl;//无友元,error:不可访问
}
void test01() {
	Building b;
	goodFriend(&b);
}
int main() {
	test01();
}

类做友元

class Building {
	friend class GoodFriend;
public:
	Building();
public:
	string m_SittingRoom;
private:
	string m_Bedroom;
};
Building::Building() {//类外构造函数
	m_SittingRoom = "sittingroom";
	m_Bedroom = "bedroom";
}
class GoodFriend {
public:
	GoodFriend();
	void visit();
	Building* building;//解引用用来初始化构造函数
};
GoodFriend::GoodFriend() {
	building = new Building;//初始化指针,指向堆区的内存
}
void GoodFriend::visit() {
	cout << "friend is visiting : " << building->m_SittingRoom << endl;
	cout << "friend is visiting : " << building->m_Bedroom << endl;
}
void test01(){
	GoodFriend gf;
	gf.visit();
}
int main() {
	test01();}

成员函数做友元

friend void GoodFriend::visit();//GoodFriend类中成员函数visit作友元

运算符重载

加号运算符重载

运算符重载也可以发生函数重载

class Person {//Person p3 = p1.operator+(p2);
public:
	//Person operator+(Person& p) {//成员函数重载+号
	//	Person temp;
	//	temp.m_A = this->m_A + p.m_A;
	//	temp.m_B = this->m_B + p.m_B;
	//	return temp;
	//}
	int m_A;
	int m_B;
};
Person operator+(Person& p1, Person& p2) {//Person p3 = operator+(p1,p2);
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p2.m_B + p2.m_B;
	return temp;
}
Person operator+(Person& p1, int p2) {//Person p3 = operator+(p1,p2);
	Person temp;
	temp.m_A = p1.m_A + p2;
	temp.m_B = p2.m_B + p2;
	return temp;
}
void test01() {
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
    Person p4 = p1 + 100;
	cout << p3.m_A << endl << p3.m_B << endl;
}

:对于内置的运算符不可更改;不要滥用运算符重载

左移运算符重载

重载左移运算符配合友元可以实现输出自定义数据类型

class Person {
	friend ostream& operator<< (ostream& out, Person& p);
public:
	Person(int a, int b) {
		m_A = a;
		m_B = b;
	}
private:
	//void operator<<(Person& p) {}//因为无法实现cout在左侧,通常不会利用成员函数重载左移运算符
	int m_A;
	int m_B;
};
//只能利用全局函数重载左移运算符
ostream& operator<< (ostream & out, Person & p) {//本质 operator<<(cout,p),简化cout<<p
	out << "m_A = " << p.m_A << " m_B = " << p.m_B;
	return out;//链式编程
}
void test01() {
	Person p(10,10);
	cout << p << endl;
}

递增运算符重载

class MyInteger {
	friend ostream& operator<<(ostream& out, MyInteger mi);
public:
	MyInteger() {
		m_Num = 0;
	}
	//重载前置++运算符,使用引用可以对一个变量进行重复操作
	MyInteger& operator++() {//如果没有引用操作就会生成一个新的变量,虽然最后可以运行,但是无法保护原变量
		m_Num++;//先进行++运算
		return *this;//再将自身做一个返回
	}
	//重载后置++运算符
	MyInteger operator++(int) {//int 代表占位参数,可以用于区分前置和后置递增
		//先返回结果
		MyInteger temp = *this;//真实的m_Num值,没有被返回
		//后递增
		m_Num++;
		//最后将记录结果做返回
		return temp;//局部对象,不能用引用,否则为非法操作
	}
private:
	int m_Num;
};
ostream& operator<<(ostream& out, MyInteger mi) {//重载后置++时返回的是一个局部变量,局部变量不能用引用
	out << mi.m_Num;//error:MyInteger &mi = temp,局部变量无法被引用
	return out;
}
void test01() {
	MyInteger mi;
	cout << ++(++mi) << endl;
	cout << mi << endl;
}
void test02() {
	MyInteger mi;
	cout << mi++ << endl;
	cout << mi << endl;
}

赋值运算符重载

一个类添加4个函数,除了构造函数、析构函数、拷贝构造函数之外,还有赋值运算符operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

class Person {
public:
	Person(int age) {
		m_Age = new int(age);
	}
	~Person() {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
	}
	Person& operator=(Person&p) {
		//编译器提供一个浅拷贝 m_Age = p.m_Age;
		//应该先判断是否有属性在堆区,如果有应该先释放干净,然后再深拷贝
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);//深拷贝
		return *this;//返回对象本身
	}
	int* m_Age;
};
void test01() {
	Person p1(10);
	Person p2(20);
	Person p3(30);
	p3 = p2 = p1;
	cout << "p1.age = " << *p1.m_Age << endl;
	cout << "p2.age = " << *p2.m_Age << endl;
	cout << "p3.age = " << *p3.m_Age << endl;
}

关系运算符重载

让自定义类型对象进行对比操作

class Person {
public:
	Person(string name, int age) {
		m_Name = name;
		m_Age = age;
	}
	bool operator==(Person& p) {
		if (m_Name == p.m_Name&& m_Age == p.m_Age) {
			return true;
			
		}
		else {
			return false;
		}
	}
	string m_Name;
	int m_Age;
};
void test01() {
	Person p1("Tom", 18);
	Person p2("Tom", 18);
	if (p1 == p2) {
		cout << "p1 == p2" << endl;
	}
	else {
		cout << "p1 != p2" << endl;
	}
}

函数调用运算符重载

由于重载后使用的方式类似函数调用,因此成为仿函数,并且没有固定写法

class MyPrint {
public:
	void operator()(string test) {//重载函数调用运算符
		cout << test << endl;
	}
};
void myprint(string text) {
	cout << text << endl;
}
void test01() {
	MyPrint mp;
	mp("Hello World");//由于使用起来像函数调用,因此称为仿函数
	myprint("Hello World");
}
class MyAdd {
public:
	int operator()(int a,int b) {
		return a + b;
	}
};
void test02() {
	MyAdd ma;
	int c = ma(10, 11);
	cout << c << endl;
	//匿名函数对象
	cout << MyAdd()(100, 100) << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值