-
July 9th, 2006, 10:44 PM
#1
Analysis on Singleton class....
Dear Gurus,
Please clear my doubts. I have browsed this site regarding Singleton class, I got to know how singleton works. But there are lot of doubts raised against "Singleton Class".
class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
Singleton()
{
//private constructor
}
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};
void main()
{
Singleton obj1Singleton;
Singleton *obj2Singleton = new Singleton();
// An object of Singleton class
//case 1:
obj1Singleton.methos();
//yet another object of singleton class
//case 2:
obj2Singleton -> method();
}
Q1) But i can create multiple objects of "Singleton" class and access "method" function.
please clear my doubt.....
Thank you in advance...
-Suren
-
July 9th, 2006, 10:53 PM
#2
Re: Analysis on Singleton class....
have a look for the Following Code. and same time static Singleton* getInstance(); in your Code. Because No where i found the defenation of this Function in you Code.
Code:
class MySingleton {
private:
static MySingleton *myReference;
MySingleton()
{
}
public:
static MySingleton *getInstance()
{
if(myReference==NULL)
myReference = new MySingleton();
return myReference;
}
};
have a look here for a Singelton Class .and go with following Link this will help you to understand Singelton Class Concept.and yes don't forget to delete your myReference after Performing your work.
http://www.codeguru.com/forum/showth...ingelton+Class
Thanx
Last edited by humptydumpty; July 9th, 2006 at 10:58 PM.
-
July 10th, 2006, 12:43 AM
#3
Re: Analysis on Singleton class....
In addition to humptydumpty's example, you also have to declare the static pointer in the .cpp file.
Code:
// In .cpp file
// Not necessary requires to assign NULL because static variables are set to 0 by default.
MySingleton *MySingleton::myReference;
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
July 10th, 2006, 01:36 AM
#4
Re: Analysis on Singleton class....
Using the above class definition for a Singleton class. how do i declare an instance in my Main function?
-Suren
-
July 10th, 2006, 01:40 AM
#5
Re: Analysis on Singleton class....
Call the getInstance() member function.
Code:
class MySingleton {
private:
static MySingleton *myReference;
MySingleton1()
{
}
public:
static MySingleton *getInstance()
{
if(myReference==NULL)
myReference = new MySingleton();
return myReference;
}
};
MySingleton *MySingleton::myReference;
int main()
{
MySingleton *p = MySingleton::getInstance();
}
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
July 10th, 2006, 02:21 AM
#6
Re: Analysis on Singleton class....
Kheun,
Can you tell me about copy constructor in c++?
-Suren
-
July 10th, 2006, 02:28 AM
#7
Re: Analysis on Singleton class....
Did u Checked all of this in MSDN or in Some Good Books. if not please have a look in MSDN for these things if still some problem search on code guru if still problem Post your code with your Doubt.
Thanx
-
July 10th, 2006, 03:46 AM
#8
Re: Analysis on Singleton class....
Originally Posted by A_Surender
Kheun,
Can you tell me about copy constructor in c++?
-Suren
You can search through this forum. Here's one of the result.
http://www.codeguru.com/forum/showth...py+constructor
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
July 10th, 2006, 04:04 AM
#9
Re: Analysis on Singleton class....
humptydumpty
Please don't mind, actually i could not understand the following line of code, in Singleton class example.
MySingleton *MySingleton::myReference;
-
July 10th, 2006, 04:10 AM
#10
Re: Analysis on Singleton class....
Never mind Dude . just One Simple Question how you Decalre a Static variable in your Class . Try this and you will Know thw answer .if Still problem Please let us know
Thanx
-
July 10th, 2006, 05:33 AM
#11
Re: Analysis on Singleton class....
Originally Posted by A_Surender
humptydumpty
Please don't mind, actually i could not understand the following line of code, in Singleton class example.
MySingleton *MySingleton::myReference;
All non-const static member variables have to be declared in a .cpp file. Basically, the first part of the line, " MySingleton *" declares a pointer to MySingleton . The second part, "MySingleton::myReference" is the full-scope name of the pointer, which also tells us myReference is a member of MySingleton.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|