当前位置:网站首页>继承和友元,静态成员的关系
继承和友元,静态成员的关系
2022-07-31 23:34:00 【华为云】
5.继承与友元
友元关系不能继承,也就是说基类友元可以访问基类的私有和保护成员,但是不能访问子类私有和保护成员
例如:你爸爸的朋友你不一定认识
class Student;//声明class Person{public: //声明Display函数是Person类的友元 friend void Display(const Person& p, const Student& s);private: string _name;};class Student: public Person{private: int _stuid;};void Display(const Person& p, const Student& s){ cout << p._name << endl; //可以访问 cout << s._stuid << endl;//不可以访问}
若想让Display函数也能够访问派生类Student的私有和保护成员,只能在派生类Student当中进行友元声明
class Student : public Person{public: //声明Display函数是Student类的友元 friend void Display(const Person& p, const Student& s);protected: int _stuid; };
6.继承与静态成员
基类定义了static静态成员,则整个继承体系里面只有一个这样的静态成员。无论派生出多少个子类,都只有一个static成员实例 。
例如:当基类Person中定义了一个静态成员变量count,派生类Student和Teacher都继承了基类Person的成员,但是再整个继承体系中,只有一个静态成员变量count
我们可以通过该静态成员变量count的数值,得知创建的对象个数
由于通过派生类/基类创建一个对象,都需要调用基类的构造函数/拷贝构造函数初始化基类的部分成员,所以我们可以在基类的构造函数/拷贝构造函数中累加_count的值,就可以得知创建了多少个对象
//基类class Person{public: //基类的构造函数 Person() { ++_count; } //基类的拷贝构造函数 Person(const Person& p) { ++_count; }public: static int _count;//静态成员变量protected: string _name;};int Person::_count = 0;//静态成员变量在类外初始化//派生类class Student :public Person{private: int _stuid;};//派生类class Teacher :public Person{private: int _teaid;};void func(Student s){}//可以通过_count的大小得知创建的对象的个数int main(){ Student s1; Student s2 = s1; Teacher t1; Person p; func(s1);//传值传参也是一次拷贝构造 //_count成员是同一个 整个继承体系里面只有一个这样的静态成员 cout << Person::_count << endl;//5 cout << Student::_count << endl;//5 return 0;}
验证:整个继承体系里面只有一个这样的静态成员
//三者的地址相同cout << &Person::_count << endl;cout << &Student::_count << endl;cout << &Teacher::_count << endl;
\
边栏推荐
- C# Rectangle基本用法和图片切割
- SQL注入 Less38(堆叠注入)
- VOT2021 game introduction
- @JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
- "SDOI2016" Journey Problem Solution
- 面试突击69:TCP 可靠吗?为什么?
- 「APIO2010」巡逻 题解
- [1161. The maximum sum of elements in the layer]
- How to reduce the gap between software design and implementation
- Daily--Kali opens SSH (detailed tutorial)
猜你喜欢
一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
什么是客户画像管理?
手写一个简单的web服务器(B/S架构)
什么是动态规划,什么是背包问题
【读书笔记->数据分析】02 数据分析准备
SQL27 View user details of different age groups
日常--Kali开启SSH(详细教程)
A high-quality WordPress download site template theme developed abroad
Design of Fire and Anti-theft System Based on Single Chip GSM
如何设计高可用高性能中间件 - 作业
随机推荐
SQL27 View user details of different age groups
mysql having的用法
Mysql environment installation under Linux (centos)
NgRx 里 first 和 take(1) 操作符的区别
Shell common script: Nexus batch upload local warehouse script
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
How to reduce the gap between software design and implementation
cobaltstrike
LeetCode 第 304 场周赛
「APIO2010」巡逻 题解
When can I use PushGateway
The difference between /usr/local/bin and /usr/bin
PHP三元(三目)运算符
date命令
硬件设备计算存储及数据交互杂谈
"APIO2010" Patrol Problem Solution
C# Rectangle basic usage and picture cutting
Payment module implementation
《ArchSummit:时代的呐喊,技术人听得到》
浏览器下载快捷方式到桌面(PWA)