友元类和类中类的一些浅显理解与对比

qt中的线程重入问题 昨天晚上写程序的时候遇到了一个问题,我想在子线程中操作父线程的类成员,但是每次都是在link的时候出现access violation,把子线程类声明成父线程的友元类也没用。   很费解,因为按照操作系统原理上的解释,一个进程中的所有线程的数据是可以共享的,并且C++的类有可 阅读详情

友元了,不管是友元类还是友元函数有应该特点是 比较鲜明的,这个特点就是不管是友元类还是友元函数他们具有一个单向的关系
你可以说a是b的友元,但是你无法说b是a的友元
看一个时间日期类来理解一下吧

class date;
class time{
	friend class date;
public:
	time(int hour, int minute, int second)
		:_year(hour)
		, _minute(minute)
		, _second(second)
	{

	}
private:
	int _year;
	int _minute;
	int _second;
};
class date{
public:
	date(int year, int month, int day)
		: _year(year),
		_month(month), 
		_day(day)   {}
	void getistime(int hour, int minute ,int second)
	{
		_t._hour = hour;
		_t._mintue = minute;
		_t._second = second;

	}
private:
	int _year;
	int _month;
	int _day;
	time _t;
};
int main()
{
	date d(1200, 1, 12);
	d.getistime(12, 18, 67);
	return 0;

}

再来看一下类中类吧
类中类外部类并没有什么优先的访问权限,更不能通过外部类去调用类不类
*内部类是外部类的友元类,内部类可以通过外部类的成员来访问外部类的所有成员

class A {
private:   
	static int k;   
	int h;
public:  
		class B{
public:       
	void foo(const A& a)      
	{
				
		cout << k << endl;//OK        
		cout << a.h << endl;//OK      
	}  
		};
};

int A::k = 1;
int main()
{	
	A::B b;  
	b.foo(A());    
	return 0; 
}
银河麒麟服务器操作系统V10SP3 U盘安装中“基础软件仓库错误”的排查与修复指南 本文针对在无网络环境下使用U盘安装银河麒麟服务器操作系统V10SP3时,遇到的“基础软件仓库错误”问题,提供了详细的排查与修复指南。核心解决方案是通过进入救援模式,使用lsblk命令确定U盘实际设备路径,并在系统引导时手动添加inst.repo内核参数来指定安装源,从而绕过安装程序的自动探测故障,确保安装流程顺利进行。 阅读详情

相关推荐

ArcGIS中如何计算多个点到特定线的最短距离

现有一个点图层和一个面图层,怎么来计算这些点到这个面图层,两个面的公共边的最短直线距离呢? 1.首先得将两个图层都转为 投影坐标系 打开工具箱-数据管理工具-要素-面转线 ​ ​ 2.将面图层设为输入要素 注意要将“识别和存储面邻域信息”勾选 ​ 3.面转线成功后 打开线属性表,线图层拥有了 LEFT_FID和RIGHT_FID两个字段 ​ 4.按属性选择,表达式中输入 “LEFT_FID”<>-1 即可选出两个面的公共边 ​再将选...

GIS蒲公英的博客 6727

简单易懂的C++类的友元教程(friend)。全局函数做类友元,一个类做另一个类的友元,一个类中某个成员函数做友元。过程中顺序很重要哦。最后附有完整实现代码

我们直接让代码走起来,可以看到结果很明确。通过breather中的vist函数,可以直接访问building类的私有属性了。现在在breather类中,只有在vist函数体内才能访问building类的私有属性了。 最后附上完整代码......

qq_51004011的博客 2099

新手学习Vivado XDMA (1) - 配置详细分析

PCIe 在 Vivado XDMA 中的配置分析。

qq_18553915的博客 9713

类的友元

一、介绍 1. 引入友元的原因:属性变量是private类型,在类的外部是不能直接用 对象 . 属性 的方式访问的,用接口函数的方式获取数据会占用更多的内存,降低效率。 2. 友元关系:一个类主动声明自己的朋友类、朋友函数,特许他们访问本类的数据。于是,在友元函数中可以直接用对象 . 属性的方式访问。它适用于外界同时需要多个对象的私有属性,比如,求两点间距,而两点各为一个对象。 例:clas...

floracuu的博客 1501

2024年最全scrapy+scrapyd+gerapy 爬虫调度框架,字节跳动面试时间

scrapyd是一个用于部署和运行scrapy爬虫的程序,它允许你通过JSON API来部署爬虫项目和控制爬虫运行,scrapyd是一个守护进程,监听爬虫的运行和请求,然后启动进程来执行它们方便地控制爬虫运行直观地查看爬虫状态实时地查看爬取结果简单地实现项目部署统一地实现主机管理轻松地编写爬虫代码。

2401_84140653的博客 1204

基于ESP32-S3与模块化设计的立创桌面充电站:从硬件架构到Web控制实战

本文详细介绍了基于ESP32-S3与模块化设计的立创桌面充电站项目,涵盖从硬件架构到Web控制的全流程实战。项目采用模块化设计,支持自由组合快充模块,并通过ESP32-S3实现智能监控,用户可通过本地网页实时查看各接口电压、电流与功率数据。文章重点解析了基板、连接板与充电模块三大核心部件的设计,特别是大电流PCB的“开窗贴铜”处理技巧,并提供了固件刷写与Web控制面板的配置指南。

weixin_36122351的博客 296

友元函数的用途

  原文链接: http://blog.chinaunix.net/uid-20665441-id-305468.html 友元提供了不同类或对象的成员函数之间、类的成员函数于一般函数之间进行数据共享的机制。通过友元,一个普通函数或者类的成员函数可以访问到封装于某一类中的数据,友元函数可以通过对象名访问类的私有和保护成员。1. 友元函数#include   #include   class  ...

weixin_34380781的博客 537

基于Python的高考志愿辅助填报系统

在实际应用中,系统需要处理更复杂的数据,包括更多的高校和专业信息、更详细的录取规则、以及用户的历史选择等。系统需要提供用户友好的界面,让考生输入自己的信息和偏好,然后通过后端的模型计算出推荐的志愿列表。此外,系统还应该提供一些个性化的服务,如根据考生的输入动态调整推荐策略、保存用户的填报历史和偏好等。特征可能包括考生的分数、所在省份、文理科类别、专业偏好、高校的地理位置、排名、专业的就业前景等。3. **推荐逻辑**:根据用户的分数和专业偏好,结合历史录取数据,使用简单的算法来推荐可能录取的高校和专业。

coder_wwwdy的博客 1982

c++考点之友元

友元如题:分析知识点:“类外函数”情形最常用的情形,将其他类的成员函数用作友元友元函数如何访问所在类的私有成员?类本身的友元函数类说明为友元非传递性非交换性总结 如题: 2021年4月: 分析 题本身没啥可说的,主要是复习下使用方法。 这部分内容属于c++复习之类和对象进阶,看讲义7部分的话,类外函数访问本类的私有成员变量。不太使用,不能够理解“类外函数”。 知识点:“类外函数”情形 最常用的情形,将其他类的成员函数用作友元 如下图所示: 1.其他类的成员函数,包括构造和析构函数,都可以说明为一个类的友

guangod的博客 554

从SolidWorks到Simulink:手把手教你用URDF文件搭建六轴机械臂仿真模型(含STL文件配置避坑)

本文详细介绍了从SolidWorks到Simulink的六轴机械臂URDF仿真全流程,包括URDF文件导出、Simscape模型生成与调试、动力学参数获取与验证等关键步骤。重点解决了STL文件配置、关节接口设置等工程化难题,帮助开发者高效构建可控制的数字孪生体。通过实战案例和优化技巧,提升机械臂仿真效率与准确性。

weixin_26812607的博客 267

上午题目复习ing

无线局域网,11b是常用的,2.4ghz,后来改进5ghz,有些字段可以工作在5g的字段802.11/11a/1h/11n,,,,,11b和11g还在2.4ghz工作。数据链路层 传输服务,数据链路维护,差错控制,arp ip到mac,rarp反过来,token ring,x.25,atm,ethernet,蓝牙10m,简化因特网,分散式网,快调频,短包,点对点,点多多点,2.4ghz,最高速率1mb/s。gsm:是时分多址,900m-1800m,微蜂窝是市区,微微风是室内,伞是盲点,

weixin_47120900的博客 894

4、友元

1、C++为什么引入友元概念? 由于面向对象编程提倡“隐藏”,因此一个类或一个全局函数只能通过成员函数通过访问另一个类的私有成员。但是有些时候,两个类之间的关闭十分密切,如果只能通过成员函数访问另一个的成员变量的话很麻烦(尤其是对于习惯于C语言编程的程序员来讲)。因此,兼顾“隐藏”和方便,C++引入了一个折中的方案。便是友元。即,可以在一个类中将另外一个类另外一个类的成员函数或者是一个全局函数声明...

江南又一春 1110

27、VXLAN BGP EVPN网络的自动化部署与运维管理

本文深入探讨了VXLAN BGP EVPN网络从初始配置到运维管理的全生命周期自动化流程,涵盖Day-0设备自动上线、Day-0.5增量变更与连接验证、Day-1覆盖服务配置与计算集成,以及Day-2监控与故障排查。详细介绍了推送与拉取两种配置模型、主流工具如Cisco VTS、NFM、DCNM的功能特点,以及开源自动化工具的应用。通过流程图和命令示例,系统展示了MAC/IP学习、BGP通告和VXLAN封装的验证方法,为构建高效、可扩展的数据中心网络提供了全面的技术指导。

banana的博客 81

c++面向对象吗?不懂看这个

深入理解c++面向对象几大特性 类 继承 重载 多态 数据抽象 数据封装 抽象类及实例化 01 类 类是 C++ 的核心特性,通常被称为用户定义的类型。也就是说,它和其他基本类型一样(浅显的这么说),都是type。类定义是以关键字 class 开头,后跟类的名称。由类可以生成对象,这里,对象可以理解为变量,一种特殊类型的变量,相当于c语言中的struct结构体类型变量。 02 类成员...

沧海一粟 1579

深度学习工具包 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")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

[转]VS2010中的单元测试

本文转自:http://zxianf.blog.163.com/blog/static/30120701201101011757499/ 在VS2010中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法也能进行单元测试,并且支持数据驱动的单元测试。 1、建立单元测试项目 1.1、从被测试代码生成单元测试 ...

weixin_30563319的博客 91

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【C++】友元类 ( 友元类简介 | 友元类声明 | 友元类单向性 | 友元类继承性 | 友元类作用 | 友元类和友元函数由来 | Java 反射机制 | C / C++ 编译过程 )

一、友元类简介 1、友元类引入 2、友元类声明 3、友元类单向性 4、友元类继承性 5、友元类作用 二、友元类和友元函数由来 1、友元类和友元函数引入 2、Java 反射机制 3、C / C++ 编译过程 三、友元类代码示例

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

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

c++类——友元

如有兴趣了解更多请关注我的个人博客。

xiaohei07的博客 2140

public、protected、private、friendly 的区别

private:声明私有类,只在这个类内部可以调用,其它类不可调用。类内部指类的成员函数和定义初始化,这个限制是对类而不是对对象的。 public:声明公共类,公共类其他类都可以调用 。一个编译单元中只能有一个pubic类。

weixin_57007253的博客 962

【C++】类的友元

类的友元

yyf_优快云_blog的博客 802

C++中类的【友元】详解

在C++中,友元是一种特殊的关系,允许某个函数或类访问另一个类的私有成员和保护成员。友元打破了类的封装性,但也为实现一些复杂逻辑提供了便利。

专注于 Qt/C++ 应用开发和机器视觉领域的开源项目分享。期待与您共同进步! 2192

类的友元函数:

类的友元函数: /* 类的友元函数: 1、友元函声明在类中任何访问权限内 2、同一个对象,在main中不能访问私有,传入友元函数,就可以访问了 3、引用传递和值传递的实参,形式上是一样的 4、保护类型中的成员,友元函数也可以访问 5、友元函数中调用成员函数或者调用成员变量,都需要类对象 6、友元函数能够使用类中所有的成员,但是都需要使用类对象来调用 */ #include<iostream> using namespace std; class demo{ public: demo(

qq_44488261的博客 895

【C++ 学习总结】- 03 - 类的认识:友元关系

「 友元(friend)」是一个用以帮助其他类中的成员函数以及全局范围的函数访问当前类的 private 和 protected 成员的机制。

DaveoCKII的博客 1663

C++【类与对象】——友元

C++【友元】——串讲 Note: i.视频为黑马程序员C++视频(118-120),系列文章为视频听课笔记; ii.难度指数:+ iii.不论变量、函数名、标识符形式怎样复杂,只要我们考虑编程的本质是对内存的操作,对内存进行分析,一切逻辑都会变得清晰。

憨憨冲鸭 1074

C++语言学习笔记:类的友元

通过友元关系,一个普通函数或者类的成员函数可以访问封装于另外一个类中数据。

往美丽边上靠的博客 297

友元 / 内部类 / 匿名对象

的普通函数,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是。

Hungry_11的博客 1115

友元函数和友元类

在类中,有public和private,在实际调用时会有访问限制,友元函数可以用不属于这个类的非成员函数来访问一个类中的私有成员。1、用普通函数来作为友元函数:2、用另一个类的成员函数做友元函数:3、友元类:     类也可以声明为另一个类的友元类:                         class A                        {friend B;}即B是A的友元类...

ukston_C的博客 582
上一篇: 指针与引用的性能比较
下一篇: 只能在堆上创建对象or只能在栈上创建对象
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值