使用级联成员函数可以获得哪些好处 [英] What benefits do you get from using cascading member functions

查看:372
本文介绍了使用级联成员函数可以获得哪些好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是通过返回* this指针来遍历成员函数调用。我得到它如何工作机械,但它看起来像实现功能的一个大的时间投入获得一个线性函数调用使用点运算符的次要能力。我肯定有一些其他的好处,除了简单地缩短成员函数调用,但真的没有太多的信息有关这个问题。 google的前几页只提出了如何实现级联函数调用的说明,什么也不知道为什么,如何,或在这种技术可能是有用的。任何人都有一些有用的链接,可能覆盖这个问题更深入?感谢。

So I just went over cascading member functions calls by returning the *this pointer. I get how it works mechanically but it looks like a large time investment in implementing the functions for gaining the minor ability to one line chain function calls using the dot operator. I'm sure there must be some other benefit beyond simply shortening member function calls but there really isn't much information about this subject. The first couple pages of google only came up with instructions on how to implement cascaded function calls and nothing about why, how, or where this technique might be useful. Anyone have some useful links that might cover the subject more indepth? Thanks.

推荐答案

当操作链接有意义时,该类的用户可以获得更好的代码。例如:

The benefit is better-looking code for users of the class when chaining of the operations makes sense. For example:

 rect.left( 10 ).top( 5 ).right( 50 ).bottom( 40 );

这可能比任何一个更好:

That may be better to read than either:

 // longer code
 rect.left( 10 );
 rect.top( 5 );
 rect.right( 50 );
 rect.bottom( 40 );

或:

 // reader should remember order of parameters
 rect.move_to( 10, 5, 50, 40 ); 

这篇关于使用级联成员函数可以获得哪些好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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