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.
The inheritance part of your code it should be like this:
class B : public A
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

1
2
3
4
Class A{
Public :
Static A* getInstance();
}


A.cpp contains nothing because its a non-concrete

B.h

1
2
3
4
5
6
7

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


B.cpp

1
2
3
4
5
6

A* A::getInstance(){
    //some code.
    Return *A
}


In other folder : We call that folder as "Folder 2"

D.h

1
2
3
4
5
6
7
8
#include B.h
class D
{
	public : 

		void Initialize();
}


D.cpp

1
2
3
4
5
6
7
8

#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...
Hi ,
why are you defining the getInstance function in class B , you have to define the function in the class a itself (.cpp) file .
[code]
A* A::getInstance(){
A* a = new B();
Return a;
}
[\code]

class A is non concrete class....
give ur rply...
Non Concrete classes

A non concrete class is a class that cannot  be instantiated.


Ok fine, even if it is a non concrete class .
The class define nation should be in the class (.cpp) file itself .
Last edited on
you can do as

1
2
3
4
5
6
7
8
9
10
11
12
class B ;
Class A
{
    Public :
             Static A* getInstance()
           {
       
              A* a = new B();
                 Return a;

              }
};


1
2
3
4
5
6
7
8
9
10
11
12
13
class A;
class B: public A ;
Class A
{
    Public :
             Static A* getInstance()
           {
       
              A* a = new B();
                 Return a;

              }
};
Topic archived. No new replies allowed.