当前位置: 首页 > article >正文

C++基础 -28- 友元

友元用于访问类中的所有数据成员

类中的私有成员,类外不可访问
在这里插入图片描述
定义友元的格式(友元函数必须要在类内,声明)
在这里插入图片描述friend void show(person &b);

使用友元访问类的所有成员
在这里插入图片描述

#include "iostream"

using namespace std;

class person
{
    public:
    int a;
    private:
    int b;
    int c;
    int d;
    friend void show(person &b);
};

void show(person &b)
{
    cout << b.a << endl;
    cout << b.b << endl;
    cout << b.c << endl;
    cout << b.d << endl;
}

int main()
{
    person a;
    cout << a.a << endl;
    cout << a.b << endl;
    cout << a.c << endl;
    cout << a.d << endl;
}

http://www.kler.cn/a/153535.html

相关文章:

  • AIGC系列之:GroundingDNIO原理解读及在Stable Diffusion中使用
  • Spine深入学习 —— 换装
  • numpy知识库:深入理解numpy的repeat函数和numpy数组的repeat方法
  • 高速风梳的方案特点--【其利天下技术】
  • C语言错误处理之 “信号处理方式<signal.h>及signal函数等内置函数”
  • Spinnaker 基于 jenkins 触发部署
  • ClassCMS2.4漏洞复现
  • springboot-mongodb-连接配置
  • 【数据中台】开源项目(2)-Davinci可视应用平台
  • C++11的异步操作(std::future,std::promise,std::packaged_task,std::async )
  • kendo-splitter动态分配分隔框大小
  • 力扣labuladong——一刷day62
  • 【CAD二次开发】标注箭头,获取修改标注箭头图块
  • C++11线程以及线程同步
  • (数据结构)顺序表的插入删除
  • Mysql中的引擎介绍(InnoDB,MyISAM,Memory)
  • C# 使用PanGu分词
  • 使用MD5当做文件的唯一标识,这样安全么?
  • Redis 基本命令—— 超详细操作演示!!!
  • 【算法刷题】Day9