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

    Exclamation Singleton class with static method

    Hi
    I am trying to implement singleton class having static method.
    my requirement is as below:

    A.h
    Class A{
    Public :
    Static A* getInstance();
    }

    B.cpp
    #include A.h
    Class B : public class A{
    A* A::getInstance()
    {
    //some code.
    Return *A
    }
    }

    D.cpp
    #include B.h

    A* pter = A::getInstance() // error: 'getInstance' is not a member of 'A'

    please tell me what is wrong in this code.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Singleton class with static method

    That's the least of your problems.

    Class, Return, and Static are all lowercase.

    Also
    Code:
    return *A
    is not allowed, and missing a trailing semi colon.

    Both #includes require quotes.

    You declare getInstance, but you never implement it.

    And please use [ code] tags (without the space)

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Singleton class with static method

    Quote Originally Posted by archana07 View Post
    Hi
    I am trying to implement singleton class having static method.
    my requirement is as below:

    A.h
    Code:
    class A{
      public :
        static A* getInstance();
    };
    A.cpp
    Code:
    #include A.h
    
    //Removed the B class
    
    A* A::getInstance()
    {
         static A* my_static_single = new A;
         return my_static_single;
    }
    main.cpp
    Code:
    #include A.h
    
    int main()
    {
        A* pter = A::getInstance();
    }
    please tell me what is wrong in this code.

    These are most of the minimal changes required to make your code even compile. Is this what you wanted?

    What was class B doing here? Either:
    1- You got confused and added it.
    2- You are trying to have a virtual static, which is not possible.

    Next time, make sure you do an actual copy-paste of your code, because even a single typo can be enough to not get the answer you need.

    I wish I could explain my changes more in detail, but that would require more time than I can spare. If you need specific help, ask, and I will take the time.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Jul 2010
    Posts
    3

    Re: Singleton class with static method

    Not sure if the inheritance will work like this

    Class B : public class A{

    I've always written it

    Class B : public A{

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Singleton class with static method

    Quote Originally Posted by monarch_dodra View Post
    Code:
    A* A::getInstance()
    {
         static A* my_static_single = new A;
         return my_static_single;
    }
    If you're going to use the "static initialization" method, which is slightly flawed but usually good enough, then there's no need to get the heap manager involved at all:

    Code:
    A* A::getInstance()
    {
         static A my_static_single;
         return &my_static_single;
    }

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Singleton class with static method

    Quote Originally Posted by Lindley View Post
    If you're going to use the "static initialization" method, which is slightly flawed but usually good enough, then there's no need to get the heap manager involved at all:

    Code:
    A* A::getInstance()
    {
         static A my_static_single;
         return &my_static_single;
    }
    Nice. But what would the "non-static initialization" be?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Singleton class with static method

    I called the above the static initialization method because you're relying on the fact that a static local will only be initialized once. The alternative is to explicitly check:

    Code:
    class A{
        static A* instance;
      public :
        static A* getInstance();
    };
    
    A* A::instance = NULL;
    
    A* A::getInstance()
    {
         if (!instance)
              instance = new A;
         return instance;
    }
    In particular, when properly mutex protected (not shown), this is slightly more thread-safe than the above. However, as with any singleton, you can make its initialization completely thread-safe simply by calling getInstance() in the main thread before spawning any others.

  8. #8
    Join Date
    Aug 2010
    Posts
    2

    Re: Singleton class with static method

    Last posted code is sample code so it have so many syntax error.
    The below code is a psudeo code. Since i can t share my code becasue its a confidential code.
    The Code is divided into two folders :
    First Folder contains : We call that folder as "Folder 1"

    A.h

    Code:
    Class A{
    Public :
    Static A* getInstance();
    }
    A.cpp contains nothing because its a non-concrete

    B.h

    Code:
    #include A.h
    class B : public A
    {
      // some code
    };
    B.cpp

    Code:
    A* A::getInstance(){
        //some code.
        Return *A
    }
    In other folder : We call that folder as "Folder 2"

    D.h

    Code:
    #include B.h
    class D
    {
    	public : 
    
    		void Initialize();
    }
    D.cpp

    Code:
    #include D.h
    
    D::Initialize(){
    
    	A* pter = A::getInstance() // error: 'getInstance' is not a member of 'A'
    }



    I am trying to build the code on CentOs.
    As per my requirement I will build the Folder 2 first and then Folder 1...

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Singleton class with static method

    Quote Originally Posted by archana07 View Post
    Last posted code is sample code so it have so many syntax error.
    The below code is a psudeo code. Since i can t share my code becasue its a confidential code.
    You are asking us to help you fix a C++ error, and providing pseudo code, how do you expect us to help?

    AFAIK, without the syntax errors, and provided the includes resolve, your code should work. I still don't see the point of B. I don't understand your Folder1/Folder2 story. My guess is that your includes are not resolving correctly.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Red face Re: Singleton class with static method

    Quote Originally Posted by archana07 View Post
    Last posted code is sample code so it have so many syntax error.
    The below code is a psudeo code. Since i can t share my code becasue its a confidential code.
    The Code is divided into two folders :
    First Folder contains : We call that folder as "Folder 1"
    ...
    To add to above comment:

    The

    error: 'getInstance' is not a member of 'A'

    most probably is because you have a different case somewhere in the definition of class A. Or you commented getInstance in class because of the syntax errors.

    It makes absolutely no sense to present pseudocode with C++ syntax but wrong case letters or missing semicolons.

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Singleton class with static method

    Quote Originally Posted by archana07 View Post
    Last posted code is sample code so it have so many syntax error.
    The below code is a psudeo code. Since i can t share my code becasue its a confidential code.
    You don't have to post your original code, just post a minimal example that replicates the problem (but be sure to post all the code of the example). The code you posted so far, contains too much useless things. To make a singleton you need exactly one class, you have more than one.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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