473,320 Members | 1,949 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,320 software developers and data experts.

Does an object pointer count as an instance in Singleton

7
I am creating a singleton class in C++. Everything is fine except with the object pointers. I cannot instaniate a regular object. But I can define many pointers *aaaa, *bbbb, etc. to the class and access the member functions of the class without using the public Instance() method. Does an pointer to a class an instance in a singleton? Can anybody tell me if this is a good example of singleton?



Here is my code run successfully:


#include "stdafx.h"
#include <iostream>

using namespace std;

class SysParms
{
private:
SysParms(const SysParms&);
int a;
static SysParms * instance; // added by zeng


protected:
SysParms(); //Only a SysParms member can call this
//Prevents a user from creating singleton objects
virtual ~SysParms(); //Prevents just anyone from deleting the singleton

public:
//The "official" access point.
static SysParms* Instance();
int cube (int);

};

//Use anonymous namespace to force internal linkage for instance

// namespace
// {
SysParms* SysParms::instance = 0; //Address of the singleton

// }

SysParms::SysParms()
{
a = 0;
}


int SysParms::cube(int b)
{ return b*b*b;}

SysParms::~SysParms()
{
delete instance;
instance = 0;
}

//The "official" access point
SysParms* SysParms::Instance()
{
//"Lazy" initialization. Singleton not created until it's needed
if (!instance) // if (instance == 0)
{
instance = new SysParms;
}
return instance;
}

int main()
{
SysParms* theSysParmsSingleton;
cout << "Example A: Creating only one instance" << endl;

//
// SysParms obj; //ERROR: singleton objects cannot be created by the user
//
//Use Instance() method to locate singleton.
//Instance() is static as we have no object
theSysParmsSingleton = SysParms::Instance();

cout << "theSysParmsSingleton is located at " << &theSysParmsSingleton
<< endl;

SysParms *aaaa, *bbbb, *cccc, *dddd;
cout << "theSysParmsSingleton is located at " << &aaaa << " " << &dddd
<< endl;

cout << bbbb->Instance()->cube(10) << endl;

cout<< aaaa->cube(6) << endl;

cout << SysParms::Instance()->cube(100) << endl;

return 0;
}
Jul 23 '07 #1
1 1711
weaknessforcats
9,208 Expert Mod 8TB
Not too bad. It looks like you read the article on Singleton classes in the C/C++ Articles section.

The idea of the Singleton is:
1) no one can create a Singleton object. That makes it a replacement for a global variable
2) the address of the Singleton is obtained from a global point oif access. This could be a global pointer, or more often, the Instance() method of the Singleton.
3) always a good idea to keep these objects in a namespace.

If you have many pointers tro the Singleton, then be sure each one is initialized by a call to the Instance() method.

In your example you use the Singleton to calculate the cube of an int. Most likely, this is not what you would use a Singleton for.\

Possible uses might be:
1) the path of the present working directory
2) the max numer of threads allowed in the application
3) constant values of any kind
4) a mediator for multiple threads accessing the same data concurrently


I suggest you pick up a copy of the Desing Patterns book for a lot more info. The book is described at the end of the article.
Jul 23 '07 #2

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

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
2
by: curious_one | last post by:
Hi, Is there a way to access a single instance of an object(singleton class) across different processes ? thanks curious.one
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
3
by: SK | last post by:
i have a class A and I have a singleton class B. i am accessing the object of B from A and is using B's method from most of the methods of A. Is it good practice to create the singleton Bs...
11
by: Vani Murarka | last post by:
Hi Everyone, Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
5
by: Christopher D. Wiederspan | last post by:
This is a bit tough to figure out the best way to ask, but here it goes. I've got a class, say MyObject, and everytime this class is instanciated, I actually want to get a reference to an existing...
6
by: Randy | last post by:
Hi, Given the following logic: 1. Determine if an object or process exists. 2. If no, create or launch. 3. If yes, access. Easy enough to do provided an explicit object reference is in...
5
by: Sam.Gundry | last post by:
Hi, I wish to share an object through a bunch of functions without declaring it globally. I have achieved this through the following function: VideoReceiver* getReceiver() { static...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.