当前位置:网站首页>友元函数和友元类
友元函数和友元类
2022-07-22 09:55:00 【wzh_scuec】
友元函数
C++提供友元(friend)机制,允许一个类将其非公有成员的访问权授予指定的函数或类。友元的声明只能出现在类定义的内部的任何地方,由于友元不是授予友元关系(friendship)的那个类的成员,所以它们不受访问控制的影响。通常,将友元声明放在类定义的开始或结尾。
友元可以是普通的函数,或已定义的其他类的成员函数,或整个类。将一个函数设为友元,称为友元函数(friend function),将一个类设为友元,称为友元类(friend class)。友元类的所有成员函数都可以访问授予友元关系的那个类的非公有成员。
因此,访问类非公有成员可以有两个用户:类成员和友元。
如果在一个类以外的某个地方定义了一个函数,在类定义中用friend对其进行声明,此函数就称这个类的友元函数。友元函数可以访问这个类中的私有成员。
友元函数举例:
#include<iostream>
#include<cmath>
using namespace std;
class Point {
public:
Point(int _x=0,int _y=0) : x(_x),y(_y) {
}
private:
int x,y;
friend double distance(Point& r1,Point& r2); //友元函数
};
double distance(Point& r1,Point& r2) {
double x = r2.x>r1.x ? r2.x-r1.x : r1.x-r2.x; //访问Point类私有成员
double y = r2.y>r2.y ? r2.x-r1.x : r1.x-r2.x; //访问Point类私有成员
return sqrt(x*x+y*y);
}
int main()
{
Point a(1,1),b(5,5);
cout<<distance(a,b);
return 0;
}
友元函数可以是另一个类的成员函数,称为友元成员函数。
友元成员函数举例:
class B; //类的前向声明
class A {
public:
A(int _a=0) : a(_a) {
}
void setb(B& r);
private:
int a;
};
class B {
public:
B(int _b=0) : b(_b) {
}
private:
int b;
friend void A::setb(B& r);
};
void A::setb(B& r) {
r.b=a; //访问B的私有成员
}
int main()
{
return 0;
}
友元类
不仅可以将一个函数声明为友元,还可以将一个类(如B)声明为另一个类(如A)的友元,这时类B就是类A的友元类。友元类B中的所有成员函数都是A类的友元函数,可以访问A类中的所有成员。
class 类名 {
//类体
...
friend 友类名;
};
class 友类名 {
//类体
...
};
关于友元类的说明:
(1)友元的关系是单向而不是双向的。如果声明了类B是类A的友元类,不等于类A是类B的友元类,类A中的成员函数不能访问类B中的私有数据。
(2)友元的关系不能传递或继承,如果类B是类A的友元类,类C是类B的友元类,不等于类C是类A的友元类。如果想让类C是类A的友元类,必须显式地在类A中另外声明。
注: 面对对象程序设计的一个基本原则是封装性和信息隐蔽,而友元却可以访问其他类中的私有成员,突破了封装原则。友元的使用有助于数据共享,能提高程序的效率,但也不要滥用,要在数据共享与信息隐蔽之间选择一个恰当的平衡点
边栏推荐
- Force deduction solution summary 648 word replacement
- 7.18 正方形数组的数目
- Traversing an array with a pointer
- The force deduction method summarizes the number of 1252 odd value cells
- 浏览器逻辑漏洞合集
- 万兴PDF专家v8.3.8.1253专业版
- Data Lake (18): Flink and iceberg integrate SQL API operations
- No longer clinging to products, apple cook increased the investment in American antitrust Lobbying: it spent $4.6 million in the first half of this year
- Rongyun handles political affairs: "small grid" can also achieve "big governance"
- MySQL 查看表结构最后变更时间
猜你喜欢
Jackson parsing JSON detailed tutorial
错过等一年!百度超级链数藏发行服务限时五折
The problem that double type cannot be accurately calculated
2022 centos8 Yum image installation & Alibaba cloud MySQL 5.7 tutorial and problem solving
Explain the adapter technology in NLP from simple to deep
【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
Solution to unsuccessful (invalid) password modification of MySQL
Research on the principle of Tencent persistence framework mmkv
【数据挖掘工程师-笔试】2022年SHEIN 公司
Delphi提高开发效率之GExperts专家的使用说明
随机推荐
"35 years old, I retired": This is the most reliable answer to the midlife crisis
ATR5179单刀双掷开关芯片替代AS179-92LF
Neo4j - Cypher 语法示例
pytest接口自动化测试框架 | pytest安装与规则
pytest接口自动化测试框架 | pytest参数化
蓝队资源大合集
NFT数字藏品系统搭建—app开发
The linear layout of fluent fills one line with two controls
Pytest interface automated testing framework | pytest parameterization
【commons-beanutils专题】004- BeanUtils 专题
Mail Informer
The force deduction method summarizes the k-diff number pairs in the 532 array
Why does additional currency issue cause inflation? How to cause
Force deduction solution summary 515- find the maximum value in each tree row
最强屏幕工具ShareX v14.0.1
大佬们,在使用flinksql时候使用rocksdb需要配置什么吗?或者需要什么jar包吗
Industrial router oilfield wireless monitoring
LeetCode刷题笔记
Day3:分支结构
Force deduction solution summary 558- intersection of quadtrees