java 友元函数_C++ 友元函数用法总结
C++中的友元机制允许类的非公有成员被一个类或者函数访问,友元按类型分为三种:
普通非类成员函数作为友元、类的成员函数作为友元、类作为友元。
1、友元的内容
友元包括友元的声明以及友元的定义。友元的声明默认为了extern,就是说友元类或者友元函数的作用域已经扩展到了包含该类定义的作用域,所以即便我们在类的内部定义友元函数也是没有关系的。
2、普通的非成员函数友元
这类友元函数通常是操作符,例如输入输出操作符.示例如下所示:
//OpeClass.h
#pragma once
class OpeClass
{
friend int func(const OpeClass xx);
public:
OpeClass(void);
OpeClass(int x,int y);
~OpeClass(void);
private:
int width;
int height;
};
//OpeClass.cpp
#include "OpeClass.h"
OpeClass::OpeClass(void)
{
width = 50;
height = 50;
}
OpeClass::OpeClass(int x,int y):width(x),height(y)
{
}
OpeClass::~OpeClass(void)
{
}
int func(const OpeClass xx)
{
return xx.height * xx.width;
}
//main.cpp
#include "OpeClass.h"
#include
using namespace std;
void main()
{
OpeClass XO;
cout<
system("pause");
}
3、类作为友元
类作为友元需要注意的是友元类和原始类之间的相互依赖关系,如果在友元类中定义的函数使用到了原始类的私有变量,那么就需要在友元类定义的文件中包含原始类定义的头文件。
但是在原始类的定义中(包含友元类声明的那个类),就不需要包含友元类的头文件,也不需要在类定义前去声明友元类,因为友元类的声明自身就是一种声明(它指明可以在类外找到友元类),示例程序如下所示:
//A.h
#pragma once
#include
using namespace std;
class A
{
friend class B;
public:
~A(void);
static void func()
{
cout<
}
private:
A(){};
static const A Test;
};
//A.cpp
#include "A.h"
const A A::Test = A();
A::~A(void)
{
}
//B.h
#pragma once
#include "C.h"
class B
{
public:
B(void);
~B(void);
void func(C& c);
};
//B.cpp
#include "B.h"
#include "A.h"
#include "C.h"
#include
using namespace std;
B::B(void)
{
}
B::~B(void)
{
}
void B::func(C& c)
{
cout<
A::Test.func();
c.func(A::Test);
}
//C.h
#pragma once
class A;
class C
{
public:
C(void);
~C(void);
void func(const A& a);
};
//C.cpp
#include "C.h"
#include
using namespace std;
C::C(void)
{
}
C::~C(void)
{
}
void C::func(const A& a)
{
cout<
}
//main.cpp
#include "A.h"
#include "B.h"
#include "C.h"
#include
using namespace std;
void main()
{
B b;
C c;
b.func(c);
system("pause");
}
4、类成员函数作为友元函数
这个稍微有点复杂,因为你要类成员函数作为友元,你在声明友元的时候要用类限定符,所以必须先定义包含友元函数的类,但是在定义友元的函数时候,又必须事先定义原始类。通常的做法先定义包含友元函数的类,再定义原始类,这个顺序不能乱。(如果是友元类,则没有这种这种必须)如下面所示:
//B.h
#pragma once
class A;
class B
{
public:
B(void);
~B(void);
int func(A xx);
};
//A.h
#pragma once
#include "B.h"
class A
{
friend int B::func(A xx);
public:
A(void):mx(20),my(30){}
~A(void){}
private:
int mx;
int my;
};
//B.cpp
#include "B.h"
#include "A.h"
B::B(void)
{
}
B::~B(void)
{
}
int B::func(A xx)
{
return xx.mx * xx.my;
}
//main.cpp
#include "A.h"
#include "B.h"
#include
using namespace std;
void main()
{
A a;
B b;
cout<
system("pause");
}
5、友元不具有相互性,只具有单项性
若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。
6、友元不能被继承
B是A的友元类,C是B的子类,推不出C是A的友元
7、友元不具有传递性
B是A的友元,C是B的友元,推不出C是A的友元
8、相互为友元的类
这个其实没什么好注意的,下面是实例,类A,类B互为友元
//A.h
#pragma once
class A
{
friend class B;
public:
A(void);
~A(void);
int funa(B& b);
private:
int mx;
int my;
};
//A.cpp
#include "A.h"
#include "B.h"
A::A(void)
{
mx = 10;
my = 10;
}
A::~A(void)
{
}
int A::funa(B& b)
{
return b.mb * b.mc;
}
//B.h
#pragma once
class B
{
friend class A;
public:
B(void);
~B(void);
int funb(A& a);
private:
int mb;
int mc;
};
//B.cpp
#include "B.h"
#include "A.h"
B::B(void)
{
mb = 20;
mc = 20;
}
B::~B(void)
{
}
int B::funb(A& a)
{
return a.mx *a.my;
}
//main.cpp
#include "A.h"
#include "B.h"
#include
using namespace std;
void main()
{
A a;
B b;
cout<
cout<
system("pause");
}
9、如果想要指定两个类都有成员函数作为对方的友元,那么必须第2个类是第一个类的友元
//A.h
#pragma once
// class B is a friend class of A
class A
{
friend class B;
public:
A(void):ma(10),mb(20){}
~A(void){}
int funa(B& b);
private:
int ma;
int mb;
};
//B.h
#pragma once
#include "A.h"
// A's function funa is a friend function of B
class B
{
friend int A::funa(B& b);
public:
B(void);
~B(void);
int funb(A& a);
int func(A& a);
private:
int mx;
int my;
};
//A.cpp
#include "A.h"
#include "B.h"
int A::funa(B& b)
{
return b.mx * b.my;
}
//B.cpp
#include "B.h"
B::B(void):mx(12),my(15)
{
}
B::~B(void)
{
}
int B::funb(A& a)
{
return a.ma + a.mb;
}
int B::func(A& a)
{
return a.ma * a.mb;
}
//main.cpp
#include "A.h"
#include "B.h"
#include
using namespace std;
void main()
{
A a;
B b;
cout<
cout<
cout<
}
java 友元函数_C++ 友元函数用法总结相关推荐
- java 友元函数_C++友元函数(超详细)
私有成员对于类外部的所有程序部分而言都是隐藏的,访问它们需要调用一个公共成员函数,但有时也可能会需要创建该规则的一项例外. 友元函数是一个不属于类成员的函数,但它可以访问该类的私有成员.换句话说,友元 ...
- vue函数如何调用其他函数?_C语言函数学习-函数的嵌套调用
文章logo 函数的嵌套调用 C语言中不允许作嵌套的函数定义.因此各函数之间是平行的,不存在上一级函数和下一级函数的问题.但是C语言允许在一个函数的定义中出现对另一个函数的调用.这样就出现了函数的嵌套 ...
- java友元类_C++友元(Friend)用法实例简介
相对于Java而言,友元是C++中特有的一种元素,很多教材上对其介绍的相对较少,因此初学的时候往往不能很快掌握,本文总结了友元的用法和一些注意的地方,供大家参考借鉴.希望能对初学C++的朋友起到一点帮 ...
- c++isdigit函数_c++isdigit函数_C语言中isdigit()函数和isxdigit()函数的用法
C语言isdigit()函数:判断字符是否为阿拉伯数字头文件: #include 定义函数: int isdigit(int c); 函数说明:检查参数 c 是否为阿拉伯数字0 到9. 返回值:若参数 ...
- c++ encode 函数_C++常用函数
ctype.h 中常用函数:字符类型判断及简单转换 C 语言中 ,在 C++ 中为 本文下面列出这个头文件的常用函数.目的是了解字符判断函数和学习简洁代码编写. 注意: C++ 中的 和 头文件继承于 ...
- java有没有友元函数_c++中友元函数理解与使用
在学习c++这一块,关于友元函数和友元类,感觉还是不好理解,但是井下心来,理解,需要把我一下几点. 首先讲友元函数. (1)友元函数: 1)C++中引入友元函数,是为在该类中提供一个对外(除了他自己意 ...
- 友元函数可以访问私有成员吗_C++的友元函数和友元类
C++使用类对数据进行隐藏和封装,类的数据成员一般定义为私有成员,而将提供类与外界通讯接口的成员函数定义为公有的. C++类成员的三种访问权限: public:可以被该类中的函数.子类的函数.友元函数 ...
- java友元类_友元实例:友元类及友元函数
学习了c++这么久,一直没有对友元进行了解,据说友元不是特别好用(据说,不是我说的),因此直到今天才去了解.其实友元确实不是很常用,但友元功能确实很实用,它不但能够释放类中的非公有成员,同时还能保证了 ...
- c语言的友元函数的用法,C++友元函数和友元类概念解析
一.友元函数 友元函数是一种特殊的函数,它需要在类体内进行说明,可以访问类的私有成员和保护成员,但又不是类的成员函数.友元函数的说明如下: friend 数据类型函数名(参数) 其中,friend是说 ...
最新文章
- 从芯片巨头沦落到收购对象 高通是怎样失去魔力的?
- 开源框架 ImageLoader +ListView+GridView+RecyclerView 浅解
- 【乱侃】How do they look them ?
- 查询空缺_携程旅行2021校招开启,9大类职位,1000+岗位空缺,本科及以上学历...
- Mysql对用户操作加审计功能——高级版
- MyEclipse6.5安装SVN插件的三种方法
- 57 - 算法 -贪心算法 - 区间不相交问题
- Python批量提取PDF文件中的文本
- Docker使用(三)使用Dockerfile创建镜像以及为镜像添加SSH服务
- git 设置用户只需要输入一次账号密码
- github第一步之初始化操作
- [lua]紫猫lua教程-命令宝典-L1-03-01. 闭包
- shader篇-阴影
- 【元胞自动机】基于元胞自动机模拟交通事故道路通行量matlab源码
- android 11.0 12.0设置app为默认浏览器
- 使用 Tableau 连接到 Hortonworks Hadoop Hive
- 为什么要做巧推整站快速排名SEO优化?
- 大数据培训需要有基础吗
- 名创优品很“优”也很“忧”
- Linux运维工程师有“钱途”吗?