F.Wessels
F.Wessels

Reputation: 217

Is it possible to create an object in an object which has as constructor the object from which it was created in c++

I am learning to build programs in c++ and am stuck at something basic. I use SDL2 to get inputs from and to deal with screens etc. I have defined an object "Program" and an object "EventHandler". The "EventHandler" handles all events (sends the events to lower level objects), but the "EventHandler" should also be able to create a new window, thus to access "Program".

This means I guess that "EventHandler" should be on the same level as "Program" and they should both be able to communicate with each other. Can this be done in c++ and how? Maybe there is some other more logical way in doing this.

The code below does obviously not work because of the order in which the classes are defined, and my selfmade "&this" to send the address of "program" is wrong, but it gives a nice overview of what I am trying to do.

//handles all events, is in contact with and same level as Program
class EventHandler {
    private:
    Program *program = NULL;
    SDL_Event e;
    //inputarrays
    const Uint8 *currentKeyStates;
    int mouseX = 0;
    int mouseY = 0;
    bool mousemotion = false;
    int mouseButtons[4] = {0, 0, 0, 0};

    public:
    EventHandler(Program *p) {
        program = p;        
    }
    void handleEvents() {
        while(SDL_PollEvent(&e) != 0) {

        }
    }
};

class Program {
    private:
    EventHandler *eh = NULL;
    std::vector<Window> windows;
    public:
    bool running;
    Program() {
        //Here the most basic form of the program is written
        //For this program we will use SDL2
        //Initialize SDL
        SDL_Init(SDL_INIT_EVERYTHING);

        //This program uses 1 window
        Window window(200, 200, 1200, 1000, "");
        windows.push_back(window);
        //Adds an event handler
        eh = new EventHandler(&this);

        //now simply run the program
        run();

    }
    void run() {
        running = true;

        //while (running) {

        //}
        SDL_Delay(2000);

        delete eh;
        //Quit SDL subsystems 
        SDL_Quit();
    }
};

int main( int argc, char* args[]) {

    Program program;

    return 0;
}

Upvotes: 0

Views: 63

Answers (2)

Tadeusz Kopec for Ukraine
Tadeusz Kopec for Ukraine

Reputation: 12413

What you need is forward declaration of Program

class Program;

class EventHandler 
{
private:
    Program *program;
....
};

class Program
{
....
};

Such declaration allows you to declare pointers to Program objects before the type is fully defined.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

Yes, it's possible, and you're close!

this is already a pointer. It's [not] already "the address of the Program".
When you write &test you're obtaining a pointer to a pointer, which is not what you want.

So you'd just write:

new EventHandler(this);

Now I'm not saying that this sort of tight coupling is a good idea, but I admit I've done similar things in the past and it can work to an acceptable degree.

Your design would be clearer and cleaner if you instead took whatever resources you want to share out of Program and literally shared them between the two classes.

Upvotes: 3

Related Questions