Jump to content
  • Advertisement

Archived

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

Rhuantavan

Classes, composition and a big mess...

This topic is 6103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! I have a little big question about classes, composition and global visibility... quite a mess Here''s my situation: I have a class Game, which via composition embeds an Input, an Engine and other classes that make up my windows program. The Game object is defined inside WinMain. Now, I need to call a method (let''s call it Input::InputMethod) inside windows'' message processing callback function, which is obviously outside WinMain''s scope. To call the InputMethod (defined as a member of the Input class) from the callback function, I needed to make the InputMethod static, so I can access it from anywhere in my program. Now the question: Is there a better way to make the Input class'' member functions callable (is that a word?) from any other part of the program without making those functions static. In game programming, is it a good choice to use composition for objects like in my primer above? I mean, how can I design my class hierarchy, so that I will be able to use any method inside other classes? I thought to make each class independent of any other, but if I think in a OOP way, then it makes sense to have this main Game object, and underneath other objects that make up a game. Does it all make sense? Damjan Mozetic Rhuantavan Project

Share this post


Link to post
Share on other sites
Advertisement
something like an input engine, mouse object etc....anything declared once in a program could be a singleton object. just search for a singleton design pattern on google. i know flipcode has some under tip of the day. But singletons are classes where one instance is created...i''m rambling and sounding redundant, so i''m gonna leave the rest up to you

Share this post


Link to post
Share on other sites
I''m by no means an expert but if it was me I''d just pass a reference to your game object to your wndproc function. Or am I saying something stupid here?

- seb

Share this post


Link to post
Share on other sites
Yes, I am aware of the singleton object, but I wasn''t so sure if there was a better way of solving this kind of problem.

The winproc is surely a function that you declare, but windows defines which parameters or not you can pass to it... I think.

Still, I''ll check this out.

Thank you.

Share this post


Link to post
Share on other sites
Either make the one instance of your Game object global, or make it a singleton you can get an instance of from anywhere by calling some static getInstance() type method. Then access it via wndproc that way.

To some degree this sort of thing seems kludgy in an OO system when you''re trying to make things as pure-OO as you can, but sometimes you have to do such things when working with legacy interfaces like wndproc''s.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!