Sign in to follow this  
Concentrate

Design Issue

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
[code]
class MusicPlayer{
//...
};
extern MusicPlayer musicPlayer
[/code]
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this