Connecting Tech Pros Worldwide Forums | Help | Site Map

question on instances

Alan Lee
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi, I am wondering if there is a way to create a class where only one
predefined instance is allowed. I am just trying to find a way to create a
global variable with its own member functions whhich would be accessible by
all other classes. I could just follow the normal class thing but I'm
wondering if there is a better way to do it.thanks .



Pete
Guest
 
Posts: n/a
#2: Jul 22 '05

re: question on instances


Alan Lee wrote:[color=blue]
> Hi, I am wondering if there is a way to create a class where only one
> predefined instance is allowed. I am just trying to find a way to
> create a global variable with its own member functions whhich would
> be accessible by all other classes. I could just follow the normal
> class thing but I'm wondering if there is a better way to do
> it.thanks .[/color]

Google for the "Singleton pattern", or put your data and functions into a
namespace.

- Pete


Pete
Guest
 
Posts: n/a
#3: Jul 22 '05

re: question on instances


Alan Lee wrote:[color=blue]
> Hi, I am wondering if there is a way to create a class where only one
> predefined instance is allowed. I am just trying to find a way to
> create a global variable with its own member functions whhich would
> be accessible by all other classes. I could just follow the normal
> class thing but I'm wondering if there is a better way to do
> it.thanks .[/color]

Google for the "Singleton pattern", or put your data and functions into a
namespace.

- Pete


jeffc
Guest
 
Posts: n/a
#4: Jul 22 '05

re: question on instances



"Alan Lee" <alanlee@stanford.edu> wrote in message
news:c4sb6f$9rh$1@news.Stanford.EDU...[color=blue]
> Hi, I am wondering if there is a way to create a class where only one
> predefined instance is allowed. I am just trying to find a way to create[/color]
a[color=blue]
> global variable with its own member functions whhich would be accessible[/color]
by[color=blue]
> all other classes. I could just follow the normal class thing but I'm
> wondering if there is a better way to do it.thanks .[/color]

Search the web for the "singleton" pattern. Typically, you must make the
constructor private or protected, and then provide a static function that
users can call to get a new object. It will only allow 1 object to exist at
a time.


jeffc
Guest
 
Posts: n/a
#5: Jul 22 '05

re: question on instances



"Alan Lee" <alanlee@stanford.edu> wrote in message
news:c4sb6f$9rh$1@news.Stanford.EDU...[color=blue]
> Hi, I am wondering if there is a way to create a class where only one
> predefined instance is allowed. I am just trying to find a way to create[/color]
a[color=blue]
> global variable with its own member functions whhich would be accessible[/color]
by[color=blue]
> all other classes. I could just follow the normal class thing but I'm
> wondering if there is a better way to do it.thanks .[/color]

Search the web for the "singleton" pattern. Typically, you must make the
constructor private or protected, and then provide a static function that
users can call to get a new object. It will only allow 1 object to exist at
a time.


Closed Thread