lundi 27 décembre 2021

Why use this pattern of having a pointer to a class? [closed]

Python programmer totally new to c++. Rewriting https://github.com/karpathy/scriptsbots as my first project. You may be able to follow this question without having to read the actual code.

The author uses a pattern that I can't understand.

  • He defines a GLView class (here)
  • Then rather than using the class directly, he creates a pointer to an instance of the class like so GLView* GLVIEW = new GLView(0);. Also worth noting that GLView.h forward declares GLVIEW (here).
  • And rather than making use of the class methods directly, he defines functions that call the methods via the pointer to the instance. For example: void gl_changeSize(int w, int h) {GLVIEW->changeSize(w,h)}.

I suppose there must be some purpose for all this that I'm not seeing yet. So what's this pattern for? Is it common? Does it have a name?


If helpful, here's what's going on in my head: why not

  1. Make an instance of GLView like this: glView = GLView();
  2. Call its methods like this: glView.gl_changeSize()

Aucun commentaire:

Enregistrer un commentaire