C ++:使用'。'表达式和函数调用的运算符 [英] C++: Using '.' operator on expressions and function calls

查看:194
本文介绍了C ++:使用'。'表达式和函数调用的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否使用成员运算符 像这样:

I was wondering if it is good practice to use the member operator . like this:

someVector = (segment.getFirst() - segment.getSecond()).normalize().normalCCW();

只是为了显示两个不同的东西,我想知道,即如果使用(表达式).member / function() foo.getBar()。getmoreBar()符合可读性和可维护性。在我从中学到的所有c ++代码和书中,我从来没有见过它以这种方式使用,但它令人陶醉的容易使用它。不想发展任何不良习惯。

Just made that to show the two different things I was wondering, namely if using (expressions).member/function() and foo.getBar().getmoreBar() were in keeping with the spirit of readability and maintainability. In all the c++ code and books I learned from, I've never seen it used in this way, yet its intoxicatingly easy to use it as such. Don't want to develop any bad habits though.

可能比这更重要,或者更少,我也想知道是否会有任何性能收益/损失使用它在这种方式,或不可预见的陷阱,将引入错误程序。

Probably more (or less) important than that, I was also wondering if there would be any performance gains/losses by using it in this fashion, or unforeseen pitfalls that would introduce bugs into the program.

提前感谢!

推荐答案


<

好吧,可能的陷阱是


  1. 更难调试。您将无法查看每个函数调用的结果,因此如果其中一个返回了意外事件,您将需要将其拆分成更小的段以查看发生了什么。此外,链中的任何调用都可能会完全失败,因此您可能必须将其分解才能找出哪个调用失败。

  1. Harder to debug. You won't be able to view the results of each function call, so if one of them is returning something unexpected you will need to break it up into smaller segments to see what is going on. Also, any call in the chain may fail completely, so again, you may have to break it up to find out which call is failing.

(有时)。链接函数调用可能使代码更难读。这取决于情况,这里没有硬而快的规则。如果表达式有点复杂,它可以使事情难以跟随。我没有任何问题,看你的具体例子。

Harder to read (sometimes). Chaining function calls can make the code harder to read. It depends on the situation, there's no hard and fast rule here. If the expression is even somewhat complex it can make things hard to follow. I don't have any problem reading your specific example.

这最终归结于个人喜好。我不努力在一条线上尽可能多的,我已经咬了足够多的时间通过链接,我不应该,我倾向于打破了一点。然而,对于不太可能失败的简单表达式,链接是好的。

It ultimately comes down to personal preference. I don't strive to fit as much as possible on a single line, and I have been bitten enough times by chaining where I shouldn't that I tend to break things up a bit. However, for simple expressions which are not likely to fail, chaining is fine.

这篇关于C ++:使用'。'表达式和函数调用的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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