Quote:Original post by visage
Its in this line:
return &sprite[spriteCounter++];
Think: The ++ is a postfix increment, right? It increments the variable AFTER the statement...but return is called first, and the function returns early, so the ++ never gets called. basically, it says this:
return &sprite[spriteCounter];spriteCounter = spriteCounter + 1;
An option is that you could set your initial index as -1, and do a ++spriteCounter instead of spriteCounter++.
...are you smoking something? Or do you not really mean "++ never gets called". Good thinking, but you're wrong. Try this:
#include <iostream>using namespace std;int foo = 1;int bar( void ){ return foo++;}int main ( int argc , char ** argv ){ cout << bar() << endl; cout << bar() << endl;}
According to your theory as stated, the program should spit out 1 for both results. It does not. Incrementation does not occur after the statement, it occurs directly at that point of code.
Take a look at the iterator classes for how this works. You'll notice they're implemented similar to so:
iterator temp = *this;++(this->position);return temp;
This is the same kind of behavior as happens with integers - as such:
int temp = spriteCounter;++spriteCounter;return &sprite[ temp ];
Quote:Original post by startreky498
I've created a structure containing a 4-member vertex array and an image. My programming method forces me to send void pointers back for logging (so that I can change my graphics class without having to edit the whole program).
Here's how it looks:
*** Source Snippet Removed ***
Thoughts so far: you shouldn't be mantaining a seperate count. If you need to know how many elements are in the vector, call sprite.size() (that should be named sprites, plural, btw). If you want the last element, either: sprites[ sprites.size() - 1 ], or sprites.back(); will do nicely. I.e. that return statement should read: "return &(sprites.back());".
Quote:When I refer back to that sprite with (SPRITE*)imagePointer after only one SPRITE created, everything works fine. The problem occurs after a second sprite is created. With the first member of the array, when I use a void pointer to recall the position of the structure, the spriteTexture member gains an (probably) invalid address of 0xfeeefeee. Why am I losing the value of my spriteTexture after pushing a new SPRITE into the vector?
*** Source Snippet Removed ***
image->spriteTexture, with the previous texture, loses its valid address for some reason. Only the most recently created member of the vector retains its texture address.
Are you sure it was valid in the first place? Have you tried stepping through your code? I'm seeing nothing obviously buggy with your code, only a couple cases of bad naming and one of bad practice. Care to post more of the source?
[Edited by - MaulingMonkey on May 14, 2005 9:20:26 PM]