C ++内联方法和与之相关的理论 [英] C++ inline methods and theory related with them

查看:61
本文介绍了C ++内联方法和与之相关的理论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在很长一段时间后潜入OOP,我偶然发现了另一个好的做法,它们可以让你的代码看起来更清洁等等。



所以内联方法。



我知道内联方法不仅是声明的方法,而且还在类本身中定义。



我听说只有当我们有更小的代码时我们才应该内联定义它们。



所以我的问题是?



什么意思是什么时候说小方法?



这是否意味着构造函数,析构函数等方法没有任何实际意义在他们中,坦率地说,我们可以创建一个初始化列表,并且在构造函数中没有单个参数。



但是这又意味着分离代码,更长的方法。 cpp和

小一点内联.h



哪个看起来更干净?更具可读性?



我尝试过:



尝试了两种方法,并且想知道哪个看起来更干净。

So I've dived into OOP after quite some time, and I stumbled upon another one of these "good practices" that help you make your code look cleaner better etc.

So inline methods.

I know that inline methods are methods that are not just declared but also defined in the class itself.

I heard we should define them inline only if we have smaller pieces of code.

So my question is?

What is meant when said smaller methods?

Does it mean constructors, destructors, etc. methods without anything really in them, quite frankly we can create an initialization list and have no single parameter inside a constructor.

But then again that would mean separating the code, longer methods in .cpp and
smaller just inline in the .h

Which looks cleaner? more readable?

What I have tried:

Tried both methods, and would like to know which looks cleaner.

推荐答案

内联函数与看起来更干净或更具可读性无关 - 它们是性能调优特征。基本上,内联函数告诉编译器我将要做很多事情,我需要快速 - 所以它不会增加参数传递和函数调用的开销,但每当你调用函数时将实际代码直接插入到当前函数中,而不是堆叠参数,保存返回地址和调用方法。它以代码大小为代价减少了执行时间,因为每次调用函数时都会插入相同的代码。因此,在一个循环中调用它数千次,您可以节省很少或没有代码大小的时间。从你代码中的数千个不同的地方调用它,你会节省一点时间 - 但可能不那么明显 - 代价是你最终应用程序中数千份相同代码的副本。



这就是为什么你只将它用于小块代码:因为否则它可以放大你的可执行文件而不会获得真正的收益。



所以:

小代码,通常在循环中调用,很少被称为一次性 - 然后使其内联。

大型代码,从很多地方调用,很少被称为在循环中 - 不要内联它。
Inline functions have nothing to do with "looking cleaner", or "more readable" - they are a performance tuning feature. Basically, an inline function is telling the compiler "I'm going to be doing this a lot and I need it quick" - so it doesn't add the overhead of parameter passing and a function call, but whenever you call the function is inserts the actual code directly into your current function instead of stacking parameters, saving the return address, and calling the method. It decreases execution time, at the expense of code size, because every time you call the function the same code is inserted. So call it thousands of times in a single loop and you save time for little or no code size cost. Call it from thousands of different places in your code, and you will save a little time - but it may not be that noticeable - at the cost of thousands of copies of the same code being in your final application.

That's why you only use it for small pieces of code: because otherwise it can enlarge your executable for no real gain.

So:
Small code, often called in a loop, rarely called as a one off - then make it inline.
Large code, called from lots of places, rarely called in a loop - don't inline it.


这篇关于C ++内联方法和与之相关的理论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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