singleton class

I want to initialize MsgQ object with argument as some message queue name.

One soloution i have is to declare
MsgQ *msgQ;
and do a new wherever i want to use by creating object with argumentarised constructor. {explained in usage example below}

But is there a better way of doing it. {Some thing like initializer list} But this is singleton class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   class Master
   {
       public:
           static Master& getInstance(const string& msgQname);
       private:
           Master();
           ~Master();

           MsgQ msgQ;  // need to initialize with message queue name (strig) in    
                       // its constructor

           MsgQ *msgQ;  // pointer as per my solution. But what if i want to intialize object above

/*Do not get confused in above two declaration. You can treat them as OR i want tips from u is it possible to initialize "MsgQ msgQ"*/

           string msgQname;  // My solution as mentioned 
   }
   
// defination

Master& Master::getInstance(const string& msgQname)
{
    msgQname = msgQname;
    static Master master;
    return master;
}

// My solution : Usage
msgQ = new MsgQ(msgQname);
Last edited on
Everytime you new up one of your objects it'll be a completely different object though? i.e. not a singleton at all.
Please see the static key word.. I guess it should be singleton
I see your static keyword but in your solution you arent using your GetInstance() method to get at your singleton are you?
Last edited on
I don't understand exactly what you're trying to accomplish, but here are some things I noticed:

-Your declaration of GetInstance() and its implementation don't match. One takes in 0 arguments while the other expects a string.
-Your Master class is indeed a singleton class though I believe singleton classes usually return a pointer to the object instead of a reference.
-Line 23 I think is illegal because a non-static data member is used without specifying the instance.
http://stackoverflow.com/questions/4130547/c-can-a-static-member-variable-call-non-static-member-functions
Ben Voigt wrote:
...static member functions have no this pointer...(emphasis added)

-Where exactly is line 29 supposed to be?

Can you clarify what it is you're asking?

Edit:
Going to take a stab at guessing what you're asking. If you're asking how to initialize data members in a singleton class, one solution would be to simply provide arguments through both the GetInstance() function and a Master constructor, e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Master{
   public:
         //Provided a default argument so function can be called without parameters
      static Master& GetInstance(const string& = "");
   private:
      Master(const string&);
//...
     MsgQ msgq;
//... 
};

Master& Master::GetInstance(const string& Qname){
   static Master singleton(Qname);
   return singleton;
}

Master::Master(const string& Qname)
   : msgq(Qname)
   , msgQname(Qname)
{}
Last edited on
Master& Master::GetInstance(const string& Qname){
static Master singleton(Qname);
return singleton;
}

Master::Master(const string& Qname)
: msgq(Qname)
, msgQname(Qname)
{}

Thanks for the above soluntion.. I was in impression since the constructor is private for singleton class above may not work...

I accpet the line 23 is illigeal.




Topic archived. No new replies allowed.