Archived

This topic is now archived and is closed to further replies.

darrylsh

Theoretical OOP/C++ Questions re: an App Class

Recommended Posts

darrylsh    163
Hi all, I have created an app class and a basic win32 wrapper class. Concerning the App class 1st. What(where) is the best / preferred way to declare an instance of that object of the following choices: app MyApp; int winmain(...) { app.run() } or int winmain(...) { app MyApp; app.run() } or same as above instead using new: app* Myapp = new app; app->run(); delete app; or is it all just a matter of individual taste/style? Thanks in Advance, Darryl [edited by - darrylsh on May 27, 2004 12:39:09 PM] [edited by - darrylsh on May 27, 2004 12:39:40 PM]

Share this post


Link to post
Share on other sites
rypyr    252
No single answer can result here. Except maybe "it depends".

My preference is to make the App class a singleton, provide a static method that can give all other classes access to the app, then reference it in main to instantiate it and delete it at end of main.

If you're not going the singleton approach and no other classes need access to the app object, then declaring it inside main is fine. Otherwise you will more than likely need a way for other classes in your program to access the application instance, so the global variable approach is one way to do this.

The pointer approach may help you if there are things about the application instantiation that can only be determined at runtime (for instance if initialization method of the application is dependent upon a command-line argument for instance). Or if you want to delay creation of the application before some other object (for instance a logger or whatever). In these instances you need to control when (and possibly how) the application is initialized and the global variable or singleton may not suffice.

Thus, the new/delete approach is probably the most flexible but also the most dangerous (you have to make sure you delete the app object even in exceptional cases. hint: use a smart pointer:


int main(int argc, char** argv) {
std::auto_ptr<app> papp(new app);

return 0;
} // at this point the app will be deleted



Regards,
Jeff

Edit: Had to change code to source tags.


[ CodeDread ]



[edited by - rypyr on May 27, 2004 12:53:52 PM]

Share this post


Link to post
Share on other sites
antareus    576
My two cents:
Don''t use a singleton, use static member functions. The singleton has a place, but it is one of the most misused design patterns around.

Share this post


Link to post
Share on other sites
rypyr    252
quote:
Original post by antareus
My two cents:
Don''t use a singleton, use static member functions. The singleton has a place, but it is one of the most misused design patterns around.


Not this again...don''t turn his post into a flamewar against/for singletons.


[ CodeDread ]

Share this post


Link to post
Share on other sites
darrylsh    163
Well I don''t have plans on redistributing my App class it''s mostly a good starting point for me to start my programs so making it a singleton is useless to me. I will always know to just use one.

As far as other objects interacting with my app class. There are no other objects in the same scope as my app class. In other words every object I use is owned by the app class.

Thanks for the replies.

Darryl

Share this post


Link to post
Share on other sites
antareus    576
Look at how MFC structures things. For every application you make you can just derive from the app class and add in the objects that are owned by the class. In this case you can just allocate the derived app class on the stack in WinMain.

Share this post


Link to post
Share on other sites
C-Junkie    1099
quote:
Original post by rypyr
Thus, the new/delete approach is probably the most flexible but also the most dangerous (you have to make sure you delete the app object even in exceptional cases.


No. You don't.

Quit spreading lies.

[edited by - C-Junkie on May 27, 2004 5:07:10 PM]

Share this post


Link to post
Share on other sites
My suggestion: don''t wrap the win32 api at all, keep it how it is, in c-style. For you main app window you can have the app_init and app_run methods in Main, but they''re not objects, just have them be global functions, simple, and doesn''t make you tear out your hair trying to wrap it and force it to conform OOP if it doesn''t take advantage of any features of OOP. This is me after trying to wrap that mess up myself; it was futile and pointless... (for my relatively small projects).

Share this post


Link to post
Share on other sites