Increment variable inside class only increments once

OK. I’m going slightly crazy with this.

I have a variable inside a class that I want to increment on update. I try using


But all it does it increment it once and then stays the same value on each update. Read up on C++ increments but still no joy. If I use ++ in the ofApp class it works fine. What’s the deal with ++ and – in class files?

Hope someone can help- if I don’t solve this soon someone is going to get hurt!


can you please post a small example?

sure thing:

class Geoloc {

    void update();
    void draw();
    int alpha; // the thing I want to decrement
    float x;
    float y;
    float z;
    ofColor fillColor;
    vector<ofColor> colorPalette;

And the implementation

Geoloc::Geoloc() {
    x = Utils::snapToGrid(ofRandom(ofGetWidth()), GEOLOCSIZE);
    y = Utils::snapToGrid(ofRandom(ofGetHeight()), GEOLOCSIZE);
    z = Utils::snapToGrid(ofRandom(-1000,300), GEOLOCSIZE);
    colorPalette = Colors::getColorPalette();
    //fillColor =;
    fillColor = ofColor(100,100,100);
    alpha = 255;

void Geoloc::update(){

    fillColor = ofColor(100,100,100,alpha);

void Geoloc::draw(){
    ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
    ofDrawPlane(x, y,z, GEOLOCSIZE, GEOLOCSIZE);

I’m trying to decrement the alpha value as opposed to increment but the idea is still the same!

Thanks Zach.

can I see how you use Geoloc ?

void ofApp::addGeoloc(){
    Geoloc g = Geoloc();

I think that looks ok - how are you iterating the geolocs vector in ofApp::update()? Something like this?

for (std::vector<Geoloc>::iterator it = geolocs.begin(); it != geolocs.end(); ++it) {

I’m doing this but I’m doing it all in ofApp::draw() (is that the problem?)

 for (int i=0; i < geolocs.size(); i++) {
        Geoloc g =;

that is making a copy of the original object in the vector and incrementing that so in each frame you are creating a new object instead of incrementing the original

 for (int i=0; i < geolocs.size(); i++) {



 for (int i=0; i < geolocs.size(); i++) {
        Geoloc & g = geolocs[i];


updates the original object. note the & when retrieving the object from the vector in the second version. it’s called a reference in c++ and it’s a way of making a variable that points to the original object. if you do:

Geoloc g =;

without the & you are copying the object

Thanks Arturo - worked perfectly! Need to get up to speed with c++

Thanks everyone else for replying too - much appreciated.

just to add on to what arturo wrote, it’s useful to get a handle of “pass by reference” and “pass by copy” – c++ is default using pass by copy, so when you put an object into a function or return it from a function, it’s making a duplicate, which is unlike Java, for example. Getting a sense of those passing semantic can be really helpful.

some useful references (no pun!) :

1 Like

Thanks Zach - had no idea about that.