views:

99

answers:

2

Hi,

I have a project with two header files mainwindow.h and website.h.

I want to access a method from website.cpp from inside mainwindow.cpp.

I can access any function from inside mainwindow by doing Window w then doing w->function();

However, when the function finishes from inside the mainwindow the memory for w is erased.

How do I keep w around and HOW DO I DECLARE Website w from my header file?

Thanks, V$h3r

+1  A: 

I guess your website.h should declare some kind of interface (I don't know if you're using classes or grouping your functions in a namespace, but it's not that important).

When you have declared your interface to manipulate your website, it should be pretty easy to call it's methods in window implementation, just include the header and use it.

If something goes wrong, then you'd better think of another program design / remove website <-> mainwindow coupling and leave only mainwindow <- website.

Sorry if I misunderstood you, probably you should give a better description of your problem.

Kotti
I have a method that takes time to return inside website that is asynchronous. Is there a way to connect it back to the main window? For example when website->get() finishes I want it to call a method inside of mainwindow and update the display.V$h3r
Vsh3r
If your actions are asynchronous, there are some good patterns that could help you. I would add an event queue inside your main window implementation and make your "website" calls add an event to that queue. So, concerning your example, I would add an event called EVENT_REDRAW to the queue after your get() method finishes. Your window in it's update cycle should take every queued event and somehow process it (in this case you should make something like "if window sees an EVENT_REDRAW event in it's queue, it should deque this event and redraw itself".
Kotti
Generally speaking, this is how winapi applications work, so if you are doing this the winapi way, think about posting a WM_PAINT message to your main window. Of course, this approach could break your coupling, because the "website" should know something about your main window.
Kotti
In my first approach, you could make your own class called EventQueue and then your "website" class (or what it actually is) should only have a reference to some Queue, which doesn't have to be window-bound. Therefore, what you would make in your main window code would be similiar to `website.initialize(event_queue)`, where this queue is bound to your window.
Kotti
After all, if you want to change your 'window' to something else, this would be as easy as adding the EventQueue object to that "something else". Hope this helps.
Kotti
Also, if you're facing scope issues like you've described, the best idea is to alter your application design instead of trying to solve problems, that are brought to you by your inappropriate design pattern.
Kotti
+2  A: 

Sounds like a scoping issue. Read this and this.

Also, be sure to check that your code within mainwindow is not returning a reference to a local pointer (In depth background)

Best of luck!

Andrew Bolster
Yes, this is a scope issue.
Vsh3r
If I declare Website w inside of my mainwindow.cpp can I still have it save a string with the html contents after calling w->get() from mainwindow?Thanks,V$h3r
Vsh3r
It depends on how you declare w inside mainwindow (This is a nice example of using extern : http://www.codersource.net/c/c-tutorials/c-tutorial-storage-specifiers.aspx)
Andrew Bolster