CuteAlien wrote:Why don't you test your code? You can do that even online:
http://ideone.com/ (switch to c++ on input).
I usually do run g++ on the console for quick 1-file tests (no project setup etc - just call "g++ myfile.cpp").
First bug - in your second case you don't set 10 different values. You just overwrite the same one 10 times
I assume you meant to have one member variable in Tester.
Also you can't do a "new Tester" for stack objects (that would return a pointer...). So let's give 2 answers:
When your vector looks as declared then you would do: vec.push_back(Tester(x));
In debug the compiler would add a function call for Tester::main which would slow down things a little bit. In release with optimization it would likely inline that away and the result would be the same. So it's probably just as fast (but certainly up to the compiler what to do with it).
As second answer let's use pointers (vector<Tester*>) and new. The new allocation would have a one-time cost which is noticeable. Memory allocations/deallocations are the one thing you should do always ahead in games (at level-loading time for example) so they don't occur while the game runs.
They are expensive.
There is a second cost which also matters (but often can't be avoided as it's a matter of runtime vs flexibility). Your function call would now be "vec[tryAll]->main();". So your array would contains pointers instead of the memory of the Tester member variable (which I still assume you wanted to add). And that messes with the way modern CPUs work. When a CPU requests memory it always requests a whole buffer, not just a single byte and they put it all in their cache. So when all values are behind each other like in the first example all checks will use the CPU cache. In the second example the pointers are cached, but for each pointer it has now to get the memory where it points at. And that memory is not necessarily close together so the CPU might have to refill the cache in between. And this can be expensive. Not as expensive as a memory allocation, but it still matters as that kind of code is generally run in speed-critical parts of the program (this is actually one of the major problems in the Irrlicht scene-graph architecture, but don't tell anyone).
Thanks, but I was just asking for the sake of the question. This will actually be in Java, though I had assumed classes worked similarly in C++.
I submitted a contest in California, a 2D game, and placed for a national competition being held in Chicago this summer.
After the judges had "lost" my score sheet, they'd finally emailed me a PDF of my scores - to allow me to see what I could improve on (since I will be competing against 149 other
teams and I am an individual programmer [Not that it matters, but we had 4 months to make a full game; I got the news late, created a full game in 13 days, and still won against many others]).
My lowest score was a 6/10 on programming logic. I assumed this was because instead of creating objects per "enemy" or "bullet" being shot, I was using one main class with everything being vectors.
Instead of trying to fix everything up, I decided to entirely re-write the game code (we are given 7 days to fix errors, change our code, or add extra content. I have 5 days left).
With that being said, instead of having a method named "AllBullets" and looping through each vector of bullets and changing their position, I've now created a class named "Bullets" and going to loop through each bullet calling a bullet.main() method.
I figured that now that I have a vector of bullets, then each bullet vector is being called in a loop (rather than having a ton of vectors in one main class and looping through each in that same class), this may change performance for the better or worse.
Finding the answer to my question will not change the way I'm going to code. It was just something I was thinking about and Google wouldn't help me out.
Hopefully you can read the above and understand, I'm sorry if I explained some things too difficult to interpret my full question.
Thanks for your help!
Am I doing a good thing re-writing my code using objects instead of vectors of variables?