当前位置:网站首页>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;
\
边栏推荐
- 一体化步进电机在无人机自动机场的应用
- 继承的注意事项
- Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
- 面试突击69:TCP 可靠吗?为什么?
- 简单的vim配置
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- Matlab / Arcgis处理nc数据
- IPD流程专业术语
- SQL注入 Less46(order by后的注入+rand()布尔盲注)
猜你喜欢
编译型语言和解释型语言的区别
MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
类和对象:中
Drawing process of hand-drawn map of scenic spots
C# Rectangle基本用法和图片切割
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
浏览器下载快捷方式到桌面(PWA)
Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
随机推荐
对象缓存服务的思考和实现
Handwritten a simple web server (B/S architecture)
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
消息队列消息存储设计(架构实战营 模块八作业)
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
(26) About menu of the top menu of Blender source code analysis
什么时候可以使用 PushGateway
/etc/sysconfig/network-scripts configure the network card
开源好用的 流程图绘制工具 drawio
IPD process terminology
力扣2326、197
《ArchSummit:时代的呐喊,技术人听得到》
lua入门案例实战123DIY
继承和友元,静态成员的关系
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
Web API 介绍和类型
助力数字政府建设,中科三方构建域名安全保障体系
面试突击69:TCP 可靠吗?为什么?
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix