友元函数

C++——详解类模板与友元函数 模板类的友元函数有三类: 1)非模板友元函数:友元函数不是模板函数,而是利用模板类参数生成的函数。2)约束模板友元函数:模板类实例化时,每个实例化的类对应一个友元函数。 3)非约束模板友元函数:模板类实例化时,如果实例化了n个类,也会实例化n个友元函数,每个实例化的类都拥有n个友元函数。 阅读详情
/* 04_11.cpp */
#include<iostream>
using namespace std;
#include<cmath>
class Point
{
int x, y;
public:
Point(int a=0, int b=0)
{ 
x=a; y=b;  }
~Point(){
}
void Show()
{ 
cout<<"x="<<x<<"y="<<y<<endl;   
}
int Get_x()
{  
return x;  
}
int Get_y()
{  
return y;  
}
friend double Distance(Point &p1, Point &p2);  //友元函数声明
};
double Distance(Point &p1, Point &p2)   //友元函数定义
{
return  sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{ 
Point p1, p2(1,1);
cout<<Distance(p1,p2)<<endl;
return 0;
} 
11 友元函数的使用大全 我们知道,C++的类具有封装和信息隐藏的特性。一般情况下,我们会封装public的成员函数供用户调用,而将成员变量设置为private或protected。但在一些比较复杂的业务情况下,可能需要去访问对象中大量的private或protected成员变量。如果为这些private或protected成员变量都封装public成员函数,无疑是比较麻烦的,有时候还会影响程序的执行效率。此时,友元函数就派上了用场。这就好比我们的房间安装了一个指纹锁,陌生人是无法进入房间内部的。 阅读详情

相关推荐

C++友元函数/友元类

(1)在类中用friend关键字去声明函数/类,则这个函数/类就会变成友元函数/友元类;(2)友元函数/友元类的声明位置没有要求,可以在private、protected、public权限区,效果都是一样的;(3)友元函数/友元类是单向的,A在B类中被声明为友元函数/友元类,表示A是B的友元函数/友元类,但B不是A的友元函数/友元类;(4)友元函数/友元类具有和类成员一样的权限,可以访问protected和private权限的成员,但不是类的成员;

qq_21603315的博客 1249

友元类

#include using namespace std; class D{ private: int year; int month; int day; public : D(int y,int m,int d) { cout year=y; month=m; day=d; } ~D() { cout } void Show() { cout } }

Riak, Spark, Golang, Erlang, 云存储, 云计算, 数据挖掘 635

Python xlwings报错解决:从‘NoneType‘到完美运行WPS表格的完整指南

本文深入解析了使用Python xlwings库自动化WPS表格时遇到的经典'NoneType'报错(AttributeError: 'NoneType' object has no attribute 'apps')的根源与解决方案。核心在于pywin32版本与WPS的兼容性问题,文章提供了从版本降级、依赖管理到构建健壮自动化环境的完整实战指南,帮助开发者彻底解决兼容性难题,实现稳定运行。

n4o5p6q7r的博客 303

【C++】友元函数 ( 友元函数简介 | 友元函数声明 | 友元函数语法 | 友元函数声明不受访问控制限制 | 友元函数参数要求 )

一、友元函数简介 二、友元函数声明 1、友元函数语法 2、友元函数声明不受访问控制限制 3、友元函数参数要求 4、友元函数示例 三、完整代码示例 - 友元函数

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 9681

用Python+ArcGIS分析厦门共享单车数据:5分钟搞定订单时长与距离分布的可视化

本文详细介绍了如何利用Python和ArcGIS Pro 3.x快速分析厦门共享单车数据,实现订单时长与距离分布的可视化。通过数据清洗、预处理和高级可视化技巧,揭示用户行为特征,为运营优化提供数据支持。文章还提供了环境配置、代码示例和业务洞察,帮助开发者高效完成数据分析任务。

weixin_28327051的博客 336

【Spreadsheet】香港规范结构设计电子表格库开发方案

深入理解香港本地规范特点和要求结合国际先进标准(Eurocode) 的技术内容考虑香港地理环境特殊性 (台风、地质等)建立完善的质量控制体系保持持续更新维护的能力。

hmywillstronger的博客 1361

C++友元函数

友元函数 友元函数可以访问其他类中的私有成员,有助于数据共享,提高程序效率,但它又是对封装性的一个破坏,所以程序中不能大量使用友元 对友元函数的使用,和普通函数的使用方法一样,不需要在友元函数前面加上特殊标志。但是如果该友元函数是一个类的成员函数,则使用时还是要子啊友元函数前加上所属的类名。 #include <iostream> using namespace std; //对Date类的提前引用声明 class Date; //定义Time类,声明display是其成员函数,形参是Date类

heifengli6666的博客 2896

PROFINET非周期数据通信实战:从报文解析到参数读写

本文详细解析了PROFINET非周期数据通信的实战应用,从报文结构到参数读写全流程。通过西门子PLC与驱动器的配置案例,深入讲解非周期通信在工业自动化中的关键作用,包括硬件选型、软件配置及常见问题排查,帮助工程师高效实现设备参数配置与诊断。

weixin_42522953的博客 513

【C++】友元函数

友元1.什么是友元函数 2.友元的作用 3.友元的优缺点 4.什么情况下可以使用友元 5.友元的使用 6.友元函数和成员函数的区别 背景:类具有封装性和隐藏性。类中的成员函数可以访问类的私有成员,类外代码中的函数无法访问类的私有成员;但是类外面的函数可以访问类的公有成员,但是把成员数据设置为公有,又会破坏类的隐藏性。此外,在某些情况下,尤其是多次调用成员函数时,由于参数传递,类型检查和安全性...

hgelin的博客 4225

Alphafold2/multimer本地安装避坑指南:从Conda环境配置到GPU加速实战

本文详细介绍了Alphafold2/multimer本地安装的全流程,从Conda环境配置到GPU加速实战,特别针对常见的报错处理提供了解决方案。通过硬件环境检查、依赖安装技巧、OpenMM补丁应用等步骤,帮助研究人员高效部署蛋白质结构预测工具,提升计算效率。

weixin_29309213的博客 116

Palabos User Guide中文解读 | 第五章 | Palabos编程 (跳过了第四章Palabos-Java接口)

作者的话:本人在学习palabos时,发现国内中文资料甚少,恰好网上可以直接搜到palabos user guide这种英文资料,加之时间充裕,便打算开始翻译,翻了一节后发现这可能算侵权,就比较伤脑筋,突然想到自己写中文解读即可,便有了下面的博客。 Palabos User Guide Release 1.0 r1 Copyright © 2019 University of Geneva Jul...

Yulan Fang's notes. 2298

qt 友元函数

private:public:// 构造函数// 声明友元函数// 定义友元函数// 友元函数可以访问Box类的私有成员// 调用友元函数return 0;在这个例子中,printWidth函数被声明为Box类的友元函数。因此,它可以在不违反封装性的情况下访问Box类的私有成员width。在main函数中,我们创建了一个Box对象,并通过友元函数printWidth打印了它的宽度。

xie__jin__cheng的博客 1203

ubuntu通过qemu安装银河麒麟V10_Arm_v8 (kylin_v10_飞腾/鲲鹏版)

一、windows下安装vmware虚拟机 二、ubuntu下载地址 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/20.04/ubuntu-20.04.3-live-server-amd64.iso 三、vmware中新建虚拟机,并使用ubuntu-20.04.3-live-server-amd64.iso作为启用盘 。。。在虚拟机中安装ubuntu(略) 四、配置ssh(如果ssh登录不上时配置) 修改openssh的配置 vi /etc/s

u010533742的博客 8704

类模板与友元函数

类模板与友元函数:友元函数在类外实现的举例 //============================================================================ // Name : test.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //====

qq_45755581的博客 2056

基于 Simulink 的单相 PWM 整流器(四象限变流器)在高铁辅助电源系统中的建模与仿真实战教程

这是一份基于 Simulink 的实战教程。在高铁牵引供电与辅助电源系统中,单相 PWM 整流器(又称四象限变流器,4QC)是极其核心的“绿色电源”枢纽。传统的二极管整流器虽然结构简单,但会导致严重的输入电流畸变和低功率因数,对电网造成极大的谐波污染。而 PWM 整流器通过高频主动控制,不仅能将单相交流电转换为平稳的直流电,还能实现输入电流正弦化、单位功率因数(PF=1)运行,并支持能量双向流动(如列车再生制动时的能量回馈)。本教程将带你从数学模型出发,搭建一套高性能的电压电流双闭环控制系统。

amy_mhd的博客 47

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

模板类和友元函数

非模板友元、约束(bound)模板友元、非约束(undound)模板友元。

孤云出岫, 去留一无所系. 朗镜悬空,静躁两不相干. 3731

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++ 友元函数

1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率。如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数。 具体来说:为了使其他类的成员函数直接访问该类的私有变量。即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同一函数。 实际上具体大概有下面两种情况需要使用友元函数:(1)运算符重载的某些场合需要使用友元。(2)两个类要共享数据的时候。 1.2使用友元函数的优缺点 优点:能够提高效率,表达简单、

weixin_43107240的博客 1238

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

友元函数,友元类

友元函数: 知识点1.友元函数的作用:该函数可以直接访问对象的私有数据成员 知识点2.现在假设有一个类A,有一个函数B,函数B在类A外(可以在其他类内,也可以是普通的全局函数),在类A中用friend对函数B进行声明,则函数B就是类A的友元函数。从而类A的友元函数B就可以访问类A的私有数据成员 知识点3.某个类的友元函数在类的公有区声明和私有区声明,效果是一样的 知识点4.友元函数的声明: 在类A...

qq_45739945的博客 774

c++友元函数

1.友元类 因为B类是友元类,所以B类的成员函数能够访问A类的私有成员 #include<iostream> using namespace std; class A { private: void fun() { cout<<"this A private fun"<<endl; } friend cla...

qq_37451250的博客 306
上一篇: 静态成员函数
下一篇: 友元类
laohan_
博客等级 码龄15年 112粉丝 224原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值