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.
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);
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.