question on instances 
July 22nd, 2005, 08:59 AM
| | | question on instances
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 . | 
July 22nd, 2005, 08:59 AM
| | | 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 | 
July 22nd, 2005, 08:59 AM
| | | 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 | 
July 22nd, 2005, 09:00 AM
| | | 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. | 
July 22nd, 2005, 09:00 AM
| | | 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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|