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

Started by
6 comments, last by darrylsh 19 years, 10 months ago
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]
Anti-Sig: Do Not Read This Signature
Advertisement
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]
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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 ]
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

Anti-Sig: Do Not Read This Signature
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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]
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).
"I study differential and integral calculus in my spare time." -- Karl Marx

This topic is closed to new replies.

Advertisement