Special type of singleton needed

B

BigMan

I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.
 
M

msalters

BigMan said:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

How do you plan to provide the arguments? It's of course easy
to provide a static setArguments(...) method for your Singleton
class which you must call before calling the canonical
static Singleton& getInstance().

The private ctor/public static getInstance pattern guarantees
your first and third conditions, but there's no way to enforce
at compile time that setArguments is called. At best you can
throw from getInstance().

HTH,
Michiel Salters
 
S

smreddy

BigMan said:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.

class myClass
{

private:
static myClass *_instance;
int pID;
protected:
public:
myClass(int processID) :pid(processID){
}
static myClass* Instance (int processID) {
try {
_instance = new myClass(processID)
return _instance;
}catch (...)
{
}
}

static myClass* Instance () {
return _instance;
}
};

int main()
{
myClass *OBJ = myClass::Instance(10003);


//
//
//
}

hope this is what you are looking for.
 
S

smreddy

BigMan said:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.

class myClass
{

private:
static myClass *_instance;
int pID;
protected:
public:
myClass(int processID) :pid(processID){
}
static myClass* Instance (int processID) {
try {
_instance = new myClass(processID)
return _instance;
}catch (...)
{
}
}

static myClass* Instance () {
return _instance;
}
};

int main()
{
myClass *OBJ = myClass::Instance(10003);


//
//
//
}

hope this is what you are looking for.
 
I

Ioannis Vranos

BigMan said:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.


That's very easy, you will just use a static data member.
 
I

Ioannis Vranos

BigMan said:
Could you please provide us with some code to demonstrate your idea?



class SomeClass
{
static SomeClass obj;

// The rest of your code

public:
SomeClass(int) { /* ... */}


};

SomeClass SomeClass::eek:bj(4);


or


class SomeClass
{
static SomeClass *p;

// The rest of your code

public:
SomeClass(int) { /* ... */}


};

SomeClass *SomeClass::p= new SomeClass(4);
 
B

BigMan

OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.
 
I

Ioannis Vranos

BigMan said:
OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.




Make the constructor private. I did not provide a complete solution but
only a way to start. Because it looks like a homework, that's why.


class SomeClass
{
static SomeClass *p;

private:
SomeClass(int x)
{
// Process x
}

// The rest of your code

};


SomeClass *SomeClass::p= new SomeClass(4);
 
D

Default User

BigMan said:
OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.

Please include quotes with your post to provide context for your
replies.



Brian
 
B

BigMan

OK, this is all fine. There is, however, one minor peculiarity: I must
initialize the object in the main function.
 
I

Ioannis Vranos

BigMan said:
OK, this is all fine. There is, however, one minor peculiarity: I must
initialize the object in the main function.


class SomeClass
{
static SomeClass *p;

static bool activated;

int x;

SomeClass(int newValue):x(newValue) {}


public:

static void initialise(int newValue)
{
if(!activated)
{
p= new SomeClass(newValue);

activated=true;
}
}

// The rest of your code

};


SomeClass * SomeClass::p= 0;
bool SomeClass::activated= false;


int main()
{
SomeClass::initialise(4);
}




That's the last piece of code that I provide. If you do not try
yourself, how will you learn?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top