【C++核心编程(三)】
一、C++对象模型和this指针
1.1、成员变量和成员函数分开存储
在C++中,类内的成员变量和成员函数分开存储,
只有非静态成员变量才属于类的对象上。
class Person
{
	int m_A;//非静态成员变量 - 属于类的对象上
	static int m_B;//静态成员变量 - 不属于类的对象上
	void func() {}//非静态成员函数 - 不属于类的对象上
	static void func2() {}//静态成员函数 - 不属于类的对象上
};
int Person::m_B = 10;
void test01()
{
	Person p;
	//空对象占用内存空间为:1
	//C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址
	cout << "size of p = " << sizeof(p) << endl;
}
void test02()
{
	Person p;
	cout << "size of p = " << sizeof(p) << endl;
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
} 

1.2、this指针概念
通过上述知识点我们知道在C++中成员变量和成员函数是分开存储的,每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码。那么问题是: 这一块代码是如何区分那个对象调用自己的呢?
C++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象。
this指针是隐含每一个非静态成员函数内的一种指针。
this指针不需要定义,直接使用即可。?
this指针的用途:
·当形参和成员变是同名时,可用this指针来区分
·在类的非静态成员函数中返回对象本身,可使用return *this
class Person
{
public:
	int age;
	Person(int age)
	{
		this -> age = age;
	}
	Person& PersonAddAge(const Person& p)
	{
		this->age += p.age;
		//this是指向p2的指针,而*this就是指向p2这个对象本体
		return *this;
	}
};
//this解决名称冲突问题
void test01()
{
	Person p(18);
	cout << "p的年龄:" << p.age << endl;
}
//返回对象本身用this*
void test02()
{
	Person p1(10);
	Person p2(10);
	//链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << "p2的年龄:" << p2.age << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
} 

1.3、空指针访问成员函数
 
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针。
如果用到this指针,需要加以判断保证代码的健壮性!
class Person
{
public:
	int m_Age;
	void showClassName()
	{
		cout << "this is Person class!" << endl;
	}
	void showPersonAge()
	{
		if (this == NULL)
		{
			return;
		}
		//错误原因是因为传入的指针是NULL(属性前默认有this->)
		cout << "age = " << this->m_Age << endl;
	}
};
void test01()
{
	Person* p = NULL;
	p->showClassName();
	p->showPersonAge();
}
int main()
{
	test01();
	system("pause");
	return 0;
} 


1.4、const修饰成员函数
常函数:
·成员函数后加const后我们称为这个函数为常函数
·常函数内不可以修改成员属性
·成员属性声明时加关键字mutable后,在常函数中依然可以修改?
常对象:
·声明对象前加const称该对象为常对象
·常对象只能调用常函数?
class Person
{
public:
	int m_A;
	mutable int m_B;//特殊变量,即使在常函数中,也可以修改
	void showPerson()const//常函数
	{
		//this指针的本质是指针常量,指针的指向是不可以修改的
		//const Person* const this
		//this->m_A = 100;//err - 在成员函数后加const,修饰的是this指向,让指针指向的值也不可以修改
		//this = NULL;//err - this指针是不可以修改指针指向的
		this->m_B = 10;
	}
	void func()
	{
		m_A = 100;
		cout << "fun()" << endl;
	}
};
void test01()
{
	Person p;
	p.showPerson();
}
void test02()
{
	const Person p;//常对象
	//p.m_A = 100;//err
	p.m_B = 100;//m_B是特殊值,在常对象下也可以修改
	//常对象只能调用常函数
	p.showPerson();
	//p.func();//err - 因为常对象是不允许修改属性的,如果常对象调用普通函数则可以修改属性,前后矛盾
	//普通成员函数可以修改属性
}
int main()
{
	test01();
	system("pause");
	return 0;
} 



?

二、友元
生活中你的家有客厅(Public),有你的卧室(Private)
客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去?
但是呢,你也可以允许你的好闺蜜好基友进去。?
在程序里,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术。?
友元的目的就是让一个函数或者类访问另一个类中私有成员
友元的关键字为:friend
友元的三种实现:
·全局函数做友元
//建筑物类
class Building
{
	//goodGay全局函数是Building的好朋友,可以访问Building中私有成员
	friend void goodGay(Building& building);
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";
	}
};
//全局函数
void goodGay(Building& building)
{
	cout << "好朋友的全局函数正在访问:" << building.m_SittingRoom << endl;
	cout << "好朋友的全局函数正在访问:" << building.m_BedRoom << endl;
}
void test01()
{
	Building building;
	goodGay(building);
}
int main()
{
	test01();
	system("pause");
	return 0;
} 

·类做友元
class Building
{
	//GoodGay类是本类的好朋友,可以访问本类的私有成员
	friend class GoodGay;
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";
	}
};
class GoodGay
{
public:
	Building* building;
	GoodGay()
	{
		//创建建筑物对象
		building = new Building;
	}
	void visit()//参观函数,访问Building属性
	{
		cout << "好朋友类正在访问:" << building->m_SittingRoom << endl;
		cout << "好朋友类正在访问:" << building->m_BedRoom << endl;
	}
};
void test01()
{
	GoodGay gay;
	gay.visit();
}
int main()
{
	test01();
	system("pause");
	return 0;
} 

·成员函数做友元
class Building;
class GoodGay
{
public:
	Building* building;
	GoodGay();
	void visit();
	void visit2();
};
class Building
{
	//告诉编译器GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
	friend void GoodGay::visit();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";
	}
};
GoodGay::GoodGay()
{
	building = new Building;
}
void GoodGay::visit()
{
	cout << "visit函数正在访问:" << building->m_SittingRoom << endl;
	cout << "visit函数正在访问:" << building->m_BedRoom << endl;
}
void GoodGay::visit2()
{
	cout << "visit2函数正在访问:" << building->m_SittingRoom << endl;
	//cout << "visit2函数正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
	GoodGay gay;
	gay.visit();
	gay.visit2();
}
int main()
{
	test01();
	system("pause");
	return 0;
} 
上述代码的顺序必须严格,不然则会报错。
①先声明Building类的存在(因为GoodGay中创建了Building类型的指针)
②实现GoodGay类(利用全局函数的方式来实现,类内的成员函数仅做声明)
③实现Building类
④对GoodGay类中的函数一一实现(GoodGay的构造函数必须在类外实现且要在Building类实现后,因为里面需要创建Building类的对象,如若在类内实现,前面光声明Building类不够,会报错)




三、运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
编译器给起了一个通用名称:operator运算符?
3.1、加号运算符重载
作用:实现两个自定义数据类型相加的运算
//加号运算符重载
class Person
{
public:
	int m_A;
	int m_B;
	//1.成员函数重载+号
	//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;
	//}
};
//全局函数重载+号
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_B;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//函数重载的版本
Person operator+(Person& p, int num)
{
	Person temp;
	temp.m_A = p.m_A + num;
	temp.m_B = p.m_B + num;
	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.operator+(p2);
	//全局函数重载的本质调用
	//Person p3 = operator+(p1, p2);
	Person p3 = p1 + p2;
	cout << "p3的m_A:" << p3.m_A << endl;
	cout << "p3的m_B:" << p3.m_B << endl;
	//运算符重载也可以发生函数重载
	Person p4 = p1 + 100;
	cout << "p4的m_A:" << p4.m_A << endl;
	cout << "p4的m_B:" << p4.m_B << endl;	
}
int main()
{
	test01();
	system("pause");
	return 0;
} 

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载
3.2、左移运算符重载
作用: 可以输出自定义数据类型
//左移运算符重载
class Person
{
	friend ostream& operator<<(ostream& cout, Person& p);
public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
private:
	int m_A;
	int m_B;
	//利用成员函数重载<<
	//一般不会利用成员函数重载<<运算符,因为无法实现cout在左侧
	//void operator<<(cout)// == p.operator<<(cout) 简化:p << cout
	//{
	//}
};
//全局函数重载<<
ostream& operator<<(ostream &cout , Person& p)//本质:operator<<(cout , p) - 简化:cout << p
{
	cout << "m_A = " << p.m_A << "  m_B = " << p.m_B;
	return cout;
}
void test01()
{
	Person p(10, 10);
	cout << p << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
} 

总结: 重载左移运算符配合友元可以实现输出自定义数据类型?
3.3、递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据
//重载递增运算符++
class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger myint);
private:
	int m_Num;
public:
	MyInteger()
	{
		m_Num = 0;
	}
	//重载++运算符
	//重载前置++
	MyInteger& operator++()//使用引用是为了一直对一个数据进行递增操作
	{
		//先进行++运算
		m_Num++;
		//再将自身返回
		return *this;
	}
	//后置递增返回值/前置递增返回引用 - 因为如果后置递增返回引用,那我们返回的是局部对象的引用
	//局部对象在当前函数执行完后会被释放,还要访问其引用则是非法操作
	//重载后置++
	MyInteger operator++(int)//int代表占位参数 - 可以用于区分前置和后置++
	{
		//先记录当时结果
		MyInteger temp = *this;
		//后递增
		m_Num++;
		//最后将记录结果返回
		return temp;
	}
};
//重载左移运算符
ostream& operator<<(ostream &cout , MyInteger myint)
{
	cout << myint.m_Num;
	return cout;
}
void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}
void test02()
{
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
} 

总结:前置递增返回引用,后置递增返回值
3.4、赋值运算符重载
c++编译器至少给一个类添加4个函数:
1.默认构造函数(无参,函数体为空)
 2.默认析构函数(无参,函数体为空)
 3.默认拷贝构造函数,对属性进行值拷贝
 4.赋值运算符 operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题。?


?


?
3.5、关系运算符重载
作用: 重载关系运算符,可以让两个自定义类型对象进行对比操作
class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	//重载==
	bool operator==(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}
	//重载!=
	bool operator!=(Person& p)
	{
		if (this->m_Name != p.m_Name || this->m_Age != p.m_Age)
		{
			return true;
		}
		return false;
	}
};
void test01()
{
	Person p1("Tom", 18);
	Person p2("Jerry", 18);
	if (p1 == p2)
	{
		cout << "p1和p2相等!" << endl;
	}
	if (p1 != p2)
	{
		cout << "p1和p2不相等!" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
} 
3.6、函数调用运算符重载
·函数调用运算符()也可以重载
·由于重载后使用的方式非常像函数的调用,因此称为仿函数
·仿函数没有固定写法,非常灵活
//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string print)
	{
		cout << print << endl;
	}
};
void test01()
{
	MyPrint myprint;
	myprint("Hello World!");//由于使用起来非常类似于函数调用,因此称为仿函数
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};
void test02()
{
	MyAdd myadd;
	int ret = myadd(100, 200);
	cout << "ret = " << ret << endl;
	//匿名函数对象 - 是匿名对象,同时又重载了仿函数
	cout << MyAdd()(100, 200) << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}    		本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!