Temporary Variables: Procrastination is the Thief of Time
C++ is a powerful programming language to learn, in part because it gives you full control over memory management. This is a two-edged sword, however; it lets you improve the performance of your code, but it also lets you shoot yourself in the foot. Therefore, it is important to understand the C++ compiler. This article examines how and why the compiler creates temporary objects, among other topics.
Temporary Variables: Procrastination is the Thief of Time - Back to the closest Enemy (Page 6 of 6 )
Our example function has to find the closest enemy to the player and doesn’t contain any static data, but here there is no need for static or temporary data at all. The enemies are passed into the function via a list that already exists outside the function’s scope, and we might as well work directly with that list.
The problem that immediately pops up with this function is the fact that the whole enemy list is passed by value and we have already considered the cost of creating copies of linked lists!
This is still pretty basic stuff. One thing you might not expect is that a parameter passed as const reference can perform as bad as a parameter passed by value! We’ll be looking at that in the next article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.