Declare and instantiate class with constructor?

Hi everybody,

I’m a C++ and OF beginner, but have some experience in other programming languages, mainly Python.

I’ve created two files, Mover.cpp and Mover.hpp, inside the src folder of my openFrameworks project to extend my project with a custom class.

Mover.hpp:

#ifndef Mover_hpp
#define Mover_hpp

#include "ofMain.h"


class Mover {
    
    ofVec3f pos;
    ofVec3f vel;
    ofVec3f acc;
    int r;
    
public:
    Mover(ofVec3f _pos, ofVec3f _vel, ofVec3f _acc, int _r);
 
    void draw();
    
};

#endif /* Mover_hpp */

Mover.cpp:

#include "Mover.hpp"

Mover::Mover(ofVec3f _pos, ofVec3f _vel, ofVec3f _acc, int _r) {
    pos = _pos;
    vel = _vel;
    acc = _acc;
    r = _r;
}

void Mover::draw() {
    ofSetColor(255, 100);
    ofFill();
    ofDrawCircle(pos.x, pos.y, r);
}

What I’m currently struggling with is, how to declare and instantiate a mover with constructor in ofApp.h and ofApp.cpp!

Here’s what I’m currently trying:

ofApp.h:

#pragma once

#include "ofMain.h"
#include "Mover.hpp"

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 mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		
    Mover mover(ofVec3f, ofVec3f, ofVec3f, int); // ???
    
};

ofApp.cpp:

#include "ofApp.h"

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

    Mover mover(ofVec3f(200, 200), ofVec3f(0, 0), ofVec3f(0, 0), 16); // ???
    
}

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

//--------------------------------------------------------------
void ofApp::draw(){
    mover.draw(); // Reference to non-static member function must be called ???
}

I get an error, when I try to call the draw() function of the mover instance.

Any help is greatly appreciated!

Hi, you can use an initializer list in the h file using a regular instance of that object or using a pointer which you can then initialize at any point of the runtime. Using a raw pointer is not a really good idea so try using a smart pointer as std::shared_ptr or std::unique_ptr

Using an Initializer list:
in your ofApp.h file

Mover mover = {ofVec3f(200, 200), ofVec3f(0, 0), ofVec3f(0, 0), 16};

Using a shared_ptr

in ofApp.h

shared_ptr<Mover> mover = nullptr;

inside ofApp::setup()

mover = make_shared<Mover> (ofVec3f(200, 200), ofVec3f(0, 0), ofVec3f(0, 0), 16);
2 Likes

Hi @roymacdonald,

Works like a charm! Thank you.

It seems like I still need to repeat the passing-in of class attributes with
Mover mover(ofVec3f(200, 200), ofVec3f(0, 0), ofVec3f(0, 0), 16);
in the ofApp::setup() function? Do you know why this has to be done twice?

Furthermore, how could I apply this, when I’m instantiating movers with a for loop inside the ofApp::setup() function, where attributes will change each iteration?

Unfortunately, I haven’t come across pointers on my C++ journey yet. I thus don’t really have a clue what they do. I’ll look into it though, since I suspect that these could maybe be the solution to the for loop stuff I mention above?

Thanks again!

you dont need to have that in the setup.
read about “C++ variables scope” which esentially refers to where a variable “lives”. In simple terms any variable exists within the opening { and closing } where it was declared. What’s inside the curly brackets is the variable’s scope. When the program execution goes out of a variable’s scope, that variable gets destroyed. Scopes can be nested, as you’ve probably seen a lot of nested curly brackets. If you happen to instantiate a variable with the same name as one that’s named the same on parenting scope, you’ll be creating a second variable with teh same name. which is what you are doing.

1 Like

Hi,

Got it, thanks.

Will do, but I already have to do read up on many things when it comes to C++. :sweat_smile:
Your help is super appreciated!

In principle, I already know about variable scope from Python, where things are structured much simpler.
What I get most confused about is the interplay between cpp and h files, especially when it comes to dealing with multiple files.

I see. Rookie mistake! :slight_smile:

I’ve written this, to generate a couple of movers in ofApp::setup():

for (int i = 0; i < 10; i++) {
        Mover mv = Mover(ofVec3f(100, 200), ofVec3f(0, 0), ofVec3f(0, 0), 2.0);
        movers.push_back(mv);
}

I’ve declared the movers vector in ofApp.h:

 std::vector<Mover> movers;

It seems to work, but it interests me if you see any issues with this setup?

Thanks again.

I see.
Right in python you have variables scope as well.
In very simple terms you can think of the .h file as the “Bill of materials” and index, where you say which are your class members, and the .cpp just like the actual instructions (the implementation) of what you declared in the h file.
You can put everything in the .h file if you want to.
A lot of people do it that way, but it is quite easy to get it super cluttered.
If you go a bit deeper it is not as simple as that, like when you have multiple interdependent classes, etc, but think of it just as a way to get things tidier.
So the, anything you declare in the h file is within the class’ scope, thus accessible from inside any of the class member functions.

It is totally fine.
Although, if you want to get picky, about what’s going on there, you have a large amount of memory copying. It might not have any noticeable impact right now but it could if you were dealing with large objects.
the problem is that push_back actually copies into the vector whatever you pass to it, there are several ways of dealing with this, like using pointers or emplace_back with std::move but all those fall out of the scope of your question. I just mention this so you keep it in mind for future projects, when dealing with large datatypes and vectors, where you could have a significant impact.
On the meanwhile it is fine. :smiley:
cheers

1 Like

I see! What a neat way to look at this! Thanks.

Oh, I thought so far that you were only supposed to put declarations in the h file and their implementation in the cpp one. I guess that would maybe be a way to keep things tidy for larger projects?

Yes, thanks. I finally understand that now.

Wow, okay. Thanks for mentioning this!! What is, in your opinion, a good source for finding things like these out? Or even to read about pointers and references?

Cheers! Have a nice weekend. :slight_smile:

Hi,
https://openframeworks.cc/ofBook/chapters/memory.html might be a good stating point.

As for stuff in general I end up googling most of the time. There are lots of resources about C++ but I would focus on “modern” C++.
Or simply ask here in the forum for specific things and we might be able to help
cheers

1 Like

Thanks.

Haha, yes that’s my main strategy too.

Noted!

Thanks again for all support and help! :slight_smile:

Regards.

1 Like