当前位置:网站首页>Inheritance and friend, static member relationship
Inheritance and friend, static member relationship
2022-08-01 00:15:00 【HUAWEI CLOUD】
5.继承与友元
友元关系不能继承,也就是说基类友元可以访问基类的私有和保护成员,But cannot access subclass private and protected members
例如:You don't necessarily know your dad's friends

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静态成员,则There is only one such static member in the entire inheritance hierarchy.无论派生出多少个子类,都只有一个static成员实例 .
例如:当基类PersonA static member variable is defined in count,派生类Student和Teacher都继承了基类Person的成员,But in the whole inheritance system,There is only one static member variablecount
We can pass this static member variablecount的数值,Get the number of objects created
As through the derived class/The base class creates an object,Both need to call the constructor of the base class/The copy constructor initializes some members of the base class,So we can in the base class constructor/Accumulate in the copy constructor_count的值,You can see how many objects were created
//基类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){}//可以通过_countThe size knows the number of objects createdint main(){ Student s1; Student s2 = s1; Teacher t1; Person p; func(s1);//Passing by value is also a copy construct //_countmembers are the same There is only one such static member in the entire inheritance hierarchy cout << Person::_count << endl;//5 cout << Student::_count << endl;//5 return 0;}验证:There is only one such static member in the entire inheritance hierarchy
//All three have the same addresscout << &Person::_count << endl;cout << &Student::_count << endl;cout << &Teacher::_count << endl;\
边栏推荐
- Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
- 【读书笔记->数据分析】02 数据分析准备
- Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
- The role of /etc/resolv.conf
- LeetCode--打家劫舍问题
- SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- Pylint检查规则中文版
- /usr/local/bin和/usr/bin的区别
- 命名实体识别-模型:BERT-MRC
猜你喜欢

Introduction to the five data types of Redis

Flink 1.13(八)CDC
![[Microservice] Distributed Transaction Solution - Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[Microservice] Distributed Transaction Solution - Seata

One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects

Matlab/Arcgis processing nc data

【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀

虹科分享|如何用移动目标防御技术防范未知因素

基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真

清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[微服务]分布式事务解决方案-Seata
随机推荐
EntityFramework保存到SQLServer 小数精度丢失
Mysql environment installation under Linux (centos)
【Acwing】The 62nd Weekly Game Solution
SQL injection Less54 (limited number of SQL injection + union injection)
基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真
Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
lua入门案例实战123DIY
NIO编程
NgRx 里 first 和 take(1) 操作符的区别
C# Rectangle basic usage and picture cutting
UOS统信系统 - WindTerm使用
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
消息队列存储消息数据的MySQL表格
精心总结十三条建议,帮你创建更合适的MySQL索引
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
Handwritten a simple web server (B/S architecture)
MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
助力数字政府建设,中科三方构建域名安全保障体系
SQL注入 Less38(堆叠注入)