博客
关于我
C++类和对象的学习【part3:对象特性2.0】
阅读量:393 次
发布时间:2019-03-05

本文共 5688 字,大约阅读时间需要 18 分钟。

C++类和对象的学习【part3:对象特性2.0】

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

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

只有非静态成员变量才属于类的对象上。
这里一共有四种成员:

  • 非静态成员变量
  • 静态成员变量
  • 非静态成员函数
  • 静态成员函数

具体展示代码如下:

// arrayone.cpp -- small arrays of integers#include 
using namespace std;#include
class Person{ public: Person() { mA = 0; } //非静态成员变量占对象空间 int mA; //静态成员变量不占对象空间 static int mB; //函数也不占对象空间,所有函数共享一个函数实例 void func() { cout << "mA:" << this->mA << endl; } //静态成员函数也不占对象空间 static void sfunc() { }};int main(){ cout << sizeof(Person) << endl; system("pause"); return 0;}

需要注意的是:

一个空的类,内存占用一个字节,目的是区分不同的空对象占用的内存位置。
如果含有非静态成员变量,则占用该成员变量字节数。
含有多个则累加。

this指针概念

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

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

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this

以下代码为返回对象本身的例子:

Person& PersonAddPerson(Person p)	{   		this->age += p.age;		//返回对象本身		return *this;	}

这样,调用时可采用递归式调用:

p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);

空指针访问成员函数

我们可以定义一个类的指针,但令其为空(暂时不是很理解这是干啥呢)。

这个空指针可以调用成员函数,但要注意有没有用到this指针。
程序主体如下:

//空指针访问成员函数class Person {   public:	void ShowClassName() {   		cout << "我是Person类!" << endl;	}	void ShowPerson() {   		if (this == NULL) {   			return;		}		cout << mAge << endl;	}public:	int mAge;};void test01(){   	Person * p = NULL;	p->ShowClassName(); //空指针,可以调用成员函数	p->ShowPerson();  //但是如果成员函数中用到了this指针,就不可以了}int main() {   	test01();	system("pause");	return 0;}

需要关注的点:

  • 空指针定义方式。
  • 空指针调用成员函数方式。
  • 用到成员变量,不能用空指针
  • 如果出现上述情况,应当用判断语句增加程序健壮性

const修饰成员函数

常函数:

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

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数

测试程序主体如下:

class Person {   public:	Person() {   		m_A = 0;		m_B = 0;	}	//this指针的本质是一个指针常量,指针的指向不可修改	//如果想让指针指向的值也不可以修改,需要声明常函数	void ShowPerson() const {   		//const Type* const pointer;		//this = NULL; //不能修改指针的指向 Person* const this;		//this->mA = 100; //但是this指针指向的对象的数据是可以修改的		//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量		this->m_B = 100;	}	void MyFunc() const {   		//mA = 10000;	}public:	int m_A;	mutable int m_B; //可修改 可变的};//const修饰对象  常对象void test01() {   	const Person person; //常量对象  	cout << person.m_A << endl;	//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问	person.m_B = 100; //但是常对象可以修改mutable修饰成员变量	//常对象访问成员函数	person.MyFunc(); //常对象只能调用const的函数}int main() {   	test01();	system("pause");	return 0;}

需要掌握的点:

  • 常对象的定义方式
  • 常函数的定义方式(const放在函数名之后)
  • 常对象可以修改mutable修饰的成员变量
  • 常对象只能调用常函数

全局函数作友元

友元关键字为friend

友元的种类:

  1. 全局函数作友元
  2. 类作友元
  3. 成员函数作友元

全局函数作友元

对于全局函数作友元,代码如下:

// arrayone.cpp -- small arrays of integers#include 
using namespace std;#include
class Building{ //告诉编译器 goodGay全局函数 是 Building类的好朋友,可以访问类中的私有内容 friend void goodGay(Building *building);public: Building() { this->m_SittingRoom = "客厅"; this->m_BedRoom = "卧室"; }public: string m_SittingRoom; //客厅private: string m_BedRoom; //卧室};void goodGay(Building *building)//这里用的是指针传递{ cout << "好基友正在访问: " << building->m_SittingRoom << endl; cout << "好基友正在访问: " << building->m_BedRoom << endl;}void test01(){ Building b; goodGay(&b);//传入对象的地址}int main(){ test01(); system("pause"); return 0;}

我的理解是在类中对一个全局函数进行声明,这样这个函数就可以访问该类中的私有成员,声明方式如下。

(不用考虑是什么权限)

friend void goodGay(Building *building);

另外本例中采用的是地址(指针)传递,并且传递的是类的指针,要学会这种用法。

类作友元

类作友元,使得该友元类可以访问原类中的私有成员变量,代码如下:

// arrayone.cpp -- small arrays of integers#include 
using namespace std;#include
class Building;class goodGay{ public: goodGay(); void visit();private: Building *building;//定义了Building类的指针};class Building{ //告诉编译器 goodGay类是Building类的好朋友,可以访问到Building类中私有内容 friend class goodGay;public: Building();public: string m_SittingRoom; //客厅private: string m_BedRoom; //卧室};Building::Building(){ this->m_SittingRoom = "客厅"; this->m_BedRoom = "卧室";}goodGay::goodGay(){ //这是一个初始化类的构造函数 building = new Building;//building是goodgay类中的成员,这里对他进行了初始化,并且采用的是new指针方式}void goodGay::visit(){ cout << "好基友正在访问" << building->m_SittingRoom << endl; cout << "好基友正在访问" << building->m_BedRoom << endl;//访问了私有权限}void test01(){ goodGay gg; gg.visit();}int main(){ test01(); system("pause"); return 0;}

这里我们采用了指针作为类定义,用到了new运算符,具体还不是很清楚为什么这么做。

重要的是在Building类中声明了:

friend class goodGay;

从而Building类包含了goodGay类作为友元。

成员函数作友元

成员函数是类中定义的函数,如果我们想要在一个类中使用一个函数,且用该函数访问另一个类中的私有成员,则可以将该成员函数,作为后者类中的友元,具体代码如下:

// arrayone.cpp -- small arrays of integers#include 
using namespace std;#include
class Building;class GoodGay{ public: GoodGay(); void visit(); //让visit函数可以访问Building中私有成员 void visit2(); //让visit2函数不可以访问Building中私有成员 Building *building;};class Building{ //告诉编译器,GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员 friend void GoodGay::visit();public: Building();public: string m_LivingRoom; //客厅private: string m_BedRoom; //卧室};//类外实现成员函数Building::Building(){ m_LivingRoom = "客厅"; m_BedRoom = "卧室";}GoodGay::GoodGay(){ building = new Building; //在堆区创建Building对象,并用指针building维护!!}void GoodGay::visit(){ cout << "visit 正在访问" << building->m_LivingRoom << endl; cout << "visit 正在访问" << building->m_BedRoom << endl;}void GoodGay::visit2(){ cout << "visit2 正在访问: " << building->m_LivingRoom << endl; //cout << "visit 正在访问" << building->m_BedRoom << endl;//不可以访问}void test01(){ GoodGay gg; gg.visit(); gg.visit2();}int main(){ test01(); system("pause"); return 0;}

其中,较为重要的是该句“声明”:

//告诉编译器,GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员friend void GoodGay::visit();

这样就让类GoodGay下的visit函数可以访问类Building中的私有成员变量。

值得一提的是:
将这里替换为类作友元可以实现同样的效果。
所以姑且认为成员函数做友元是一种更为精细的友元声明方式(纯属本人臆测)。

转载地址:http://upewz.baihongyu.com/

你可能感兴趣的文章
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>
mysql 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>