CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2005
    Posts
    76

    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

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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.

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  4. #4
    Join Date
    Nov 2005
    Posts
    76

    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

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  6. #6
    Join Date
    Nov 2005
    Posts
    76

    Re: Analysis on Singleton class....

    Kheun,

    Can you tell me about copy constructor in c++?

    -Suren

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Analysis on Singleton class....

    Quote 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.

  9. #9
    Join Date
    Nov 2005
    Posts
    76

    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;





  10. #10
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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

  11. #11
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Analysis on Singleton class....

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured