• Advertisement
Sign in to follow this  

Design Issue

This topic is 2466 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
Sign in to follow this  

  • Advertisement