C语言刷题集(二)

题目来源牛客网https://www.nowcoder.com

  1. 友元的作用之一是:
    a.提高程序的运行效率
    b.加强类的封装性
    c.实现数据的隐藏性
    d.增加成员函数的种类

(此正确答案来自牛客网)

解析:原思路是用排除法,

  • 因为增加成员函数种类可以多定义成员函数来解决,所以排除D;
  • 封装的意义在于隐藏数据细节,但是友元却又可以访问到类内数据,所以排除B;
  • 友元从函数调用以及其它过程来看,并没有提高程序运行效率,所以排除A;
  • 只能选C,但是友元并不能隐藏数据,反而使数据暴露了出去,因此对C还有疑惑。

但看了网友解析后发现,

友元函数的使用使得类与类之间的通讯更加方便。ID:~火车头~

质疑:这句从编译运行过程来说并没有增加效率啊,即使通讯方便,也不一定效率就增加了啊,再去找了论坛上网友关于友元的讨论“c++ 友元 friend的好处是?”,发现没有提到效率,反而是封装与隐藏的讨论更多,于是又在网上发现了这句话:

友元的正确使用能提高程序的运行效率,但破坏了类的封装性和数据的隐蔽性,导致程序可维护性变差,因此一定要谨慎使用。

最后发现这句话出自《Visual C++面向对象程序设计教程与实验》第87页,到这里,于是发现了这个题的前世今生(可能某个老程序员读到了这句话,于是把它作为考试题出给了学生,学生觉得这道题不错,于是上传到网上,接着被某个程序员爬到了题库里。)但到最后,还是对这个答案的可信度有质疑,毕竟从编译运行原理来说都不会加快效率。
最终,到官网上找到了正确答案,FAQ: friends-and-encap,其中这样描述:

Do friends violate encapsulation?
No! If they’re used properly, they enhance encapsulation.

You often need to split a class in half when the two halves will have different numbers of instances or different lifetimes. In these cases, the two halves usually need direct access to each other (the two halves used to be in the same class, so you haven’t increased the amount of code that needs direct access to a data structure; you’ve simply reshuffled the code into two classes instead of one). The safest way to implement this is to make the two halves friends of each other.

If you use friends like just described, you’ll keep private things private. People who don’t understand this often make naive efforts to avoid using friendship in situations like the above, and often they actually destroy encapsulation. They either use public data (grotesque!), or they make the data accessible between the halves via public get() and set() member functions. Having a public get() and set() member function for a private datum is okay only when the private datum “makes sense” from outside the class (from a user’s perspective). In many cases, these get()/set() member functions are almost as bad as public data: they hide (only) the name of the private datum, but they don’t hide the existence of the private datum.

Similarly, if you use friend functions as a syntactic variant of a class’s public access functions, they don’t violate encapsulation any more than a member function violates encapsulation. In other words, a class’s friends don’t violate the encapsulation barrier: along with the class’s member functions, they are the encapsulation barrier.

(Many people think of a friend function as something outside the class. Instead, try thinking of a friend function as part of the class’s public interface. A friend function in the class declaration doesn’t violate encapsulation any more than a public member function violates encapsulation: both have exactly the same authority with respect to accessing the class’s non-public parts.)

所以最终答案是选B啦,加强类的封装性!(再次吐槽一句国内教材:一点都不严谨!!!)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值