Connecting Tech Pros Worldwide Help | Site Map

question on instances

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 08:59 AM
Alan Lee
Guest
 
Posts: n/a
Default 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 .



  #2  
Old July 22nd, 2005, 08:59 AM
Pete
Guest
 
Posts: n/a
Default 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


  #3  
Old July 22nd, 2005, 08:59 AM
Pete
Guest
 
Posts: n/a
Default 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


  #4  
Old July 22nd, 2005, 09:00 AM
jeffc
Guest
 
Posts: n/a
Default 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.


  #5  
Old July 22nd, 2005, 09:00 AM
jeffc
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.