Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

Design Issue

This topic is 2588 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

I have a class called MusicPlayer. The whole application might need to access a music player. Although there could be multiple musicPlayer object, as I write the code, there will be only one music player object.
So I ask how to fit this musicPlayerObject into my application. I have a couple of ideas, maybe one of this is a good one? I need your thoughts please.

Option # 1 -- create a function in music player class to retrive a globally used object
[source]
class MusicPlayer{
//...
MusicPlayer& getObject(){ static MusicPlayer m; return m; }
};
[/source]
Option # 2 -- create a header in which a namespace manages all global variable used
[source]
namespace GlobalVariable{
MusicPlayer musicPlayer;
//...
}
[/source]

Option # 3, use extern keyword

class MusicPlayer{
//...
};
extern MusicPlayer musicPlayer

and define it in musicplayer.cpp

or some other method? Any suggestion or comments.

This problem comes up often and I would like a good solid solution.

regards, D.Chhetir

Share this post


Link to post
Share on other sites
Advertisement
Declare it as a local variable inside main, then pass a reference to the functions that use it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!