This-> keyword confusion

hello, beginner here. i have some experience with processing, lua and other scripting languages. i’m working my way slowly through the oF book and digesting some c++ basics in the meantime. i also stumbled on this site https://junkiyoshi.com/ (which is super awesome and inspiring stuff)

now my question, in his code he often uses the ‘this->’ keyword in .cpp file. from a java perspective i can understand you can use this in the class constructor. but what is its function here, is it a pointer to an already declared variable in the .h file? i couldn’t really find a clue in the oF book and stackoverflow answers are confusing me. thanks!

In C++ this is a pointer to the current instance of a class, ie the object as it is currently running.

every time you access a class member inside a class you could use this->variable however its mostly used for clarity, particularly when a class variable has the same name as a function variable, e.g.

SomeClass.h

class SomeClass {
public:
    void SomeFunction(int);
    int someVariable;

}
void SomeClass::SomeFunction(int someVariable) {
    this->someVariable = someVariable;
}

Without this its not clear to the compiler which someVariable is being referred to, the class variable or the function arg, so you need to use this-> to differentiate.

thanks @dizzy, this makes sense. but why is he using ‘this->’ straight inside the ofApp::setup() {} function which doesn’t accept function variables? example taken from first sketch here https://junkiyoshi.com/2020/01/

this->fbo.allocate(ofGetWidth(), ofGetHeight());
this->fbo.begin();
etc…

his code is full of this-> inside the setup/update functions. is this redundant/unnecessary? this is confusing the hell out of me (sorry english is not my first lang, correct me if i’m wrong)

#pragma once
#include "ofMain.h"

class ofApp : public ofBaseApp {

public:
	void setup();
	void update();
	void draw();

	void keyPressed(int key) {};
	void keyReleased(int key) {};
	void mouseMoved(int x, int y) {};
	void mouseDragged(int x, int y, int button) {};
	void mousePressed(int x, int y, int button) {};
	void mouseReleased(int x, int y, int button) {};
	void windowResized(int w, int h) {};
	void dragEvent(ofDragInfo dragInfo) {};
	void gotMessage(ofMessage msg) {};

	int number_of_agent;
	int len_of_tail;
	vector<deque<glm::vec2>> log_list;
	vector<glm::vec2> direction_list;

	ofFbo fbo;
	ofPixels pixels;
};
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup() {

	ofSetFrameRate(60);
	ofSetWindowTitle("openFrameworks");

	ofBackground(239);

	ofTrueTypeFont font;
	font.loadFont("fonts/Kazesawa-bold.ttf", 250, true, true, true);

	this->fbo.allocate(ofGetWidth(), ofGetHeight());
	this->fbo.begin();
	ofClear(0);

	string word = "PCD";
	font.drawString(word, ofGetWidth() * 0.5 - font.stringWidth(word) * 0.5, ofGetHeight() * 0.5 - font.stringHeight(word) * 0.1);
	word = "TOKYO";
	font.drawString(word, ofGetWidth() * 0.5 - font.stringWidth(word) * 0.5, ofGetHeight() * 0.5 + font.stringHeight(word) * 1.1);

	this->fbo.end();
	this->fbo.readToPixels(this->pixels);

	this->number_of_agent = 120;
	this->len_of_tail = 100;
	while (this->log_list.size() < this->number_of_agent) {

		int x = ofRandom(ofGetWidth());
		int y = ofRandom(ofGetHeight());
		if (this->pixels.getColor(x, y) != ofColor(0, 0)) {

			deque<glm::vec2> log;
			log.push_front(glm::vec2(x, y));
			this->log_list.push_back(log);
			this->direction_list.push_back(glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1))));
		}
	}

	ofSetLineWidth(1.5);
}

//--------------------------------------------------------------
void ofApp::update() {

	for (int i = 0; i < this->number_of_agent; i++) {

		glm::vec2 future = this->log_list[i].front() + this->direction_list[i] * 5;
		int x = future.x;
		int y = future.y;

		while (this->pixels.getColor(x, y) == ofColor(0, 0)) {

			this->direction_list[i] = glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1)));

			future = this->log_list[i].front() + this->direction_list[i] * 10;
			x = future.x;
			y = future.y;
		}

		this->log_list[i].push_front(future);
		while (this->log_list[i].size() > this->len_of_tail) {

			this->log_list[i].pop_back();
		}
	}
}

//--------------------------------------------------------------
void ofApp::draw() {

	for (int i = 0; i < this->number_of_agent; i++) {

		ofSetColor(39);
		for (int l = 0; l < this->log_list[i].size() - 1; l++) {

			ofDrawLine(this->log_list[i][l], this->log_list[i][l + 1]);
		}
	}
}

//--------------------------------------------------------------
int main() {

	ofSetupOpenGL(1280, 720, OF_WINDOW);
	ofRunApp(new ofApp());
}

It doesn’t have to be just function arg variables, can be local variables as well, e.g.

void SomeClass::SomeFunction(int x) {
    int someVariable = x;
    this->someVariable = someVariable;
}

(obviously nonsense code but just as an example)

Sometimes people use it just out of habit, or sometimes they just do it to be clearer to themselves.

In the body of long functions it can be helpful to be clear on what is a class variable vs a local function variable as well. I’d guess they are just doing it to be clear.

1 Like

thanks @dizzy. ok seems like it is used here to differentiate variables declared in .h vs local variables (but using it the other way around). c++ has so many little things and nuances , easy to freak out as a beginner if you see stuff like this in a slightly different context :smile:

No probs, glad it’s making sense.

Yes C++ is a big language, I find the C++ faq useful https://isocpp.org/faq (although I cant seem to find anything on this)

Scott Meyers has some good books on C++ although if you’re just looking to get enough to mess around with oF its probably too much info. I don’t think i’ve ever managed to finish one of his books but they are useful to have as a reference.