473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

creating single instance

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
2 2328
ice8595
13
i think i figured out how it works
Mar 15 '07 #2
sicarie
4,677 Expert Mod 4TB
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

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

Similar topics

8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
3
by: VooDoo | last post by:
hi, is there a way to ensure an application will run a single instance (for example, like word that opens each document in a different word programm) via mfc features or do i have to code it...
3
by: Seth | last post by:
I have been trying to create a mock httpcontext for the purpose of unit testing. I have a class, Customer, that uses cookies. I have set it up to be able to take a httpcontext as a parameter in...
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
2
by: vvenk | last post by:
Hello: I am thinking of an object called "user." This object will be instatntiated during a login process. But I want to use this to control the behavior of the application since it will also...
1
by: CES | last post by:
All, I was wondering if someone could point me to a tutorial on creating & accessing functions from within a wrapper function. I've created a group of functions related to a timer event. All...
11
by: JohnJSal | last post by:
It seems like what I want to do is something that programmers deal with everyday, but I just can't think of a way to do it. Basically, I am writing a DB front-end and I want a new "Researcher"...
8
by: arunrocks | last post by:
Hi I am having a requirement to create a db in 2 out of 8 partitiones. I have the following doubts. 1. should I create a new instance in 2 partitions alone (the present instance spans 8 nodes)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.