By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

creating single instance

P: 13
Hi there,

I'm having a little trouble getting this, so please excuse me if my question and description sounds confusing or juvenile.

I'm creating a new class called 'Rec'.
And essentially I want to reuse the same instance object (myInstance) every time. So it would be created the first time i initiate the constructor, but the following times it would just refer back to the same one.

Expand|Select|Wrap|Line Numbers
  1. //the specification
  2. class Rec {    
  3.   public:    
  4.       ~Rec();    
  5.       static Rec* instance();
  6.  
  7.   protected:  
  8.           Rec();
  9.  
  10.  private:  
  11.           static Rec* myInstance;
  12. };
  13.  
But my question is, how do I actually go ahead to declare a new Rec instance in my Main method? As in, what does that actual line of code look like? I can't figure out how to use this constructor: static Rec* instance();

Thanks a lot for your help
Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: 13
i think i figured out how it works
Mar 15 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
Hi there,

I'm having a little trouble getting this, so please excuse me if my question and description sounds confusing or juvenile.

I'm creating a new class called 'Rec'.
And essentially I want to reuse the same instance object (myInstance) every time. So it would be created the first time i initiate the constructor, but the following times it would just refer back to the same one.

Expand|Select|Wrap|Line Numbers
  1. //the specification
  2. class Rec {    
  3.   public:    
  4.       ~Rec();    
  5.       static Rec* instance();
  6.  
  7.   protected:  
  8.           Rec();
  9.  
  10.  private:  
  11.           static Rec* myInstance;
  12. };
  13.  
But my question is, how do I actually go ahead to declare a new Rec instance in my Main method? As in, what does that actual line of code look like? I can't figure out how to use this constructor: static Rec* instance();

Thanks a lot for your help
What you want is the singleton class, but you have it just a little bit backwards. You need to be able to declare an instance, but only one instance of the class.

This means the constructor needs to be private, you need a reference of the class to live locally, so that you can keep, modify, and return that instance as your singleton, you need to be able to get that instance, and then the tricky, but not too tricky part, is to create one and only one reference. Like I said, you're very close, but there are a few things you have backwards.


Expand|Select|Wrap|Line Numbers
  1. //the specification
  2. class Rec {    
  3.   public:    
  4.       ~Rec();    
  5.       static Rec* instance();
  6.       // I'd have this as getInstance() - just for "good programming practice", but that's good
  7.  
  8.  private:  
  9.           static Rec* myInstance;
  10.           // also good - this takes care of having one instance
  11.           // but you also need the constructor here, one that will only instantiate one instance of it
  12.          static Rec() {
  13.              if (myInstance == null) 
  14.                  myInstance = new Rec();
  15.              else
  16.                  return myInstance;
  17.           };
  18. };
  19.  
I'm not sure if this is the case in C++, but with Java having an omnipotent superclass of Object, you would need to overload the copy constructor, throwing an exception when they attempted to access it. (This would also require making the whole class final as well.) I'm not sure the Rec() code is 100% right, but it should definitely be enough to get you going.
Mar 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.