-
August 16th, 2010, 08:48 AM
#1
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.
-
August 16th, 2010, 09:21 AM
#2
Re: Singleton class with static method
That's the least of your problems.
Class, Return, and Static are all lowercase.
Also
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)
-
August 16th, 2010, 10:02 AM
#3
Re: Singleton class with static method
Originally Posted by archana07
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.
-
August 16th, 2010, 10:04 AM
#4
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{
-
August 16th, 2010, 10:15 AM
#5
Re: Singleton class with static method
Originally Posted by monarch_dodra
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;
}
-
August 16th, 2010, 10:34 AM
#6
Re: Singleton class with static method
Originally Posted by Lindley
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.
-
August 16th, 2010, 11:58 AM
#7
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.
-
August 17th, 2010, 01:09 AM
#8
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...
-
August 17th, 2010, 02:48 AM
#9
Re: Singleton class with static method
Originally Posted by archana07
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.
-
August 17th, 2010, 03:13 AM
#10
Re: Singleton class with static method
Originally Posted by archana07
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.
-
August 17th, 2010, 03:52 AM
#11
Re: Singleton class with static method
Originally Posted by archana07
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|