Passing vector by reference

Started by
10 comments, last by rip-off 11 years, 8 months ago
The first line of code is not legal. The fix is to store the temporary:

CollisionType collision = getTileCollision(x, y)
iMngr->collision(&camera, myCursor, &collision, player, map->getMap(), enemyVector, currentInterface, 0);


Pre C++11, you can only initialise variables in this manner, not assign to them. I'm not familiar enough with C++11 initialiser lists to comment on whether it is legal in it. I would guess you'd need to write the appropriate assignment operator.

textColor = {0, 0, 0};

To fix it, use a constructor call, declare a temporary or make a factory function (useful if you do not control the type):

textColor = Color(0, 0, 0);

// or
Color temp = { 0, 0, 0 }
textColor = temp;

// or
Color make_colour(int r, int g, int b) {
Color color = { r, g, b };
return color;
}

textColor = make_color(0, 0, 0);
Advertisement
Thank you very much. Really this helped me so much!

The reason why i didn't resolve the "taking address of temporary" before was that i thought that if i used more code it would take up more CPU power. It wont?

The reason why i didn't resolve the "taking address of temporary" before was that i thought that if i used more code it would take up more CPU power. It wont?
[/quote]
Lines of code does not 100% map to CPU usage. In any case, you probably don't want to trade performance for crashes.

Here are some performance rules of thumb:

  • Is the game/program running too slow on the target hardware? If so, continue on below. If not, then you don't need to optimise for the time being. Make the game better instead!

  • 80% of the time in your program is consumed in 20% of the code. The 20% of code is called the "bottleneck". Any time spent optimising the other 80% of the program is poorly spent.

  • Programmers are notoriously bad at guessing where these "bottlenecks" actually are. This means that even programmers who know not to optimise the 80% of their program can often end up doing so. Instead of guessing, start measuring. Learn to use a profiler.

  • When removing known bottlenecks, apply algorithmic optimisations first. In the case of collision detection, are you doing spatial partitioning?

  • Finally, if you have determined where the bottleneck lies and exhausted any algorithmic optimisations, you might then consider tricks on the level of individual lines of code like that. You need to be able to measure the performance difference with the previous implementation to ensure you are actually making a difference, and that the difference is positive!

This topic is closed to new replies.

Advertisement