当前位置:网站首页>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;
\
边栏推荐
- 【Acwing】The 62nd Weekly Game Solution
- 推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
- SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
- Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
- IPD process terminology
- LeetCode--The problem of robbery
- 编译型语言和解释型语言的区别
- LeetCode--打家劫舍问题
- 【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
- Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
猜你喜欢
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
cobaltstrike
【MATLAB项目实战】LDPC-BP信道编码
浏览器下载快捷方式到桌面(PWA)
类和对象:上
[Microservice] Distributed Transaction Solution - Seata
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Redis五种数据类型简介
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
随机推荐
新产品如何进行网络推广?
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
消息队列消息存储设计(架构实战营 模块八作业)
【Acwing】The 62nd Weekly Game Solution
/etc/resolv.conf的作用
一体化步进电机在无人机自动机场的应用
SQL injection Less38 (stack injection)
Pylint检查规则中文版
leetcode:126. 单词接龙 II
逐步手撕轮播图3(保姆级教程)
Introduction to the five data types of Redis
Network security - crack WiFi through handshake packets (detailed tutorial)
Likou Binary Tree
类和对象:上
C# Rectangle basic usage and picture cutting
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
SQL injection Less54 (limited number of SQL injection + union injection)
SQL注入 Less42(POST型堆叠注入)
【MATLAB项目实战】LDPC-BP信道编码
IPD process terminology