朋友函数和朋友类之间有什么区别? [英] what is the difference between friend function and friend class?

查看:136
本文介绍了朋友函数和朋友类之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友函数和朋友类之间有什么区别?应该使用friend关键字?

what is the difference between friend function and friend class? and where should be use of friend keyword?

推荐答案

简而言之,一个是类,一个是函数。对于函数,只有一个函数可以访问私有成员。对于一个类,整个类及其所有函数都可以访问对等类的私有成员。

In short, one is a class and one is a function. For the function, just that one function gets access to private members. For a class, the whole class and all its functions get access to the private members of the befriended class.

friend关键字用于授予对私有数据成员的访问权限。有时你可能需要一个帮助类或一个免费类来访问不同类的私有成员。对于函数,常见的例子是运算符过载。也许你想重载+运算符。您可以在类外声明一个运算符+函数(因此可以在没有对象的情况下调用它),并且需要访问私有类数据。

The friend keyword is used to grant access to private data members. At times you may need a helper class or a complimentary class to access the private members of a different class. For functions, a common example is an operator overload. Perhaps you want to overload the + operator. You may make an operator+ function declared outside the class (so it can be called without an object) and it will need to access the private class data.

签出此网站,以获取有关两者的详细说明以及如何使用它们。

Check out this site for a detailed description of both and how to use them.

这篇关于朋友函数和朋友类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆