0

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;
}
F.Wessels
  • 179
  • 11
  • 2
    Please [edit] your question and include a [mcve]. Please be concise and clear. I guess "Is it possible to create an object in an object which has as constructor the object from which it was created in c++" could be the winner of the least comprehensive question. – YSC Dec 30 '15 at 13:10
  • 1
    Are you trying to say you just want those two classes to have pointers to each other? – 15ee8f99-57ff-4f92-890c-b56153 Dec 30 '15 at 13:12
  • @ YSC You mean I should change the title to an title with an example? Then the title is to long :(. I just want to know if it is possible when you create an object in an object in c++, if you can give the first object (the second object was created from ) to the second object. – F.Wessels Dec 30 '15 at 13:16
  • I think the implication you are making is that `Program` and `EventHandler` should both have access to `std::vector windows`. This implies that `windows` is a shared resource. You could look into a Flyweight pattern to accomplish this, or alternatively decide that one or the other is the ultimate owner of `windows`, so long as you're still able to pass the data to the other when needed. This would require you write getter functions. – AndyG Dec 30 '15 at 13:17
  • 2
    Yo dawg I put an object in ur object... – Lightness Races in Orbit Dec 30 '15 at 13:17
  • What I could have done is simply let "EventHandler" be a property of "Program". Then it is easy to let "EventHandler" gain access to all windows of "Program" ( change size for example ). But I would also like that "EventHandler" can add a window. Mm, you all give reactions to fast, I need some time to think about your reactions... – F.Wessels Dec 30 '15 at 13:23

2 Answers2

3

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

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.

Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83