当前位置:网站首页>Friend function, friend class, class template
Friend function, friend class, class template
2022-04-23 06:37:00 【*Flowers bloom on the street】
C++ Provide Friends Mechanism , Allow external classes and functions to access private members of classes and helper methods that protect members , Declare them as... Of a given class Friend class ( Or friend function ), Make it have access to class member functions . But the friend itself is not a member of the class , It doesn't belong to any class .
advantage : Improve the reliability of the software 、 Reusability 、 Maintainability .
Insufficient : It increases the cost of calling functions when the program is running —— When member functions are called frequently , It will lead to the reduction of execution efficiency , Increase the running time of the program .
Friends can be ordinary functions , Or member functions of other classes that have been defined , Or entire class . Set a function as friend , be called Friend function , Set a class as a friend , be called Friend class . All member functions of a friend class can access the private members of the class that granted the friend relationship .
One 、 Friend function
Friend function : If a function is defined somewhere outside a class , Use... In the class definition friend Declare it , This function is called the of this class Friend function .
Definition form of friend function :
friend Return type Function name ( parameter list );
{ … }
explain :
1) Friend function is not a member function of a class , Private members that can access objects , Just in the class Add... To the statement friend, Because it is not a member function , It is not necessary to add... Before the function name when defining “ Class name ::”.
2) A function can be a friend function of multiple classes , Just declare it separately in each class .
3) Friend function none this The pointer , therefore , Generally, there should be an entry parameter —— The object name passed through the entry parameter refers to the member of the object .
4) When a function needs to access two or more classes , Friend functions are very useful .
5)C++ It is not allowed to declare constructors and destructors as friend functions .
friend member function : It means that the member function of one class can be a friend function of another class, that is, the private members of another class can be accessed through the member function of one class .
Two 、 Friend class
Friend class Is an access mechanism established between multiple classes , When two or more classes are defined in the program , If you want one class to have access to private members of another class , You can declare this class as a friend of another class , All member functions of a friend class can be regarded as friend functions of that class , Can access private and protected members of this class .
Define the form of friend class :
friend Class name ;
Such as : class A
{···
friend B; } ;
class B {...} ;
3、 ... and 、 About friends :
1. Friends can access and modify the private or protected data of the class , Call the private or protected member function of the class , Make the class both encapsulated , And flexible .
2. The relationship between friends is one-way rather than two-way . If a class is declared B It's a class A Friends class , It's not equal to class A It's a class B Friends class , class A Member functions in cannot access classes B Private data in .
3. A friend's relationship cannot be passed or inherited , If a class B It's a class A Friends class , class C It's a class B Friends class , It's not equal to class C It's a class A Friends class . If you want class c It's a class A Friends class , In addition, it must be explicitly declared in the class .
Four 、 Class template
about Some functions with the same function but different data types , You can define a General function template To achieve . about Some classes with the same function but different data types , You can also define a general class template .
Class template definition form :
template <class Type parameter name >
class Class name
{
···
};
The form of object defined by class template is :
Class template name < Actual type name > Object name ;
If you define member functions outside the class template , The form of class template shall be written as :
template <class Type parameter name >
Function type Class template name < Type parameter name >:: Member function name ()
{ The body of the function }
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547207002.html
边栏推荐
猜你喜欢
【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
[leetcode 54] spiral matrix
P1586 solution to tetragonal theorem
Graduation project, viewing screenshots of epidemic psychological counseling system
Basic knowledge of network in cloud computing
MySQL table constraints and table design
[leetcode 401] binary Watch
Generation of verification code
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
ArcGIS license错误-15解决方法
随机推荐
Basemap库绘制地图
-- SQL query and return limit rows
破解滑动验证码
Gesture recognition research
爬取小米有品app商品数据
Substring Inversion (Easy Version)
Introduction to virtualization features
程序設計訓練
Common shortcut keys of IDE
爬虫效率提升方法
C语言实现2048小游戏方向合并逻辑
Rust:如何实现一个线程池?
GDB debugger installation and use
定位器
1006 finding a mex (hdu6756)
Database - sorting data
Explanation of the second I interval of 2020 Niuke summer multi school training camp
Generation of verification code
word排版遇到的格式问题
小区房价可视化