By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Special type of singleton needed

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

BigMan wrote:
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

Jul 23 '05 #2

P: n/a
BigMan wrote:
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.

Jul 23 '05 #3

P: n/a
BigMan wrote:
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.

Jul 23 '05 #4

P: n/a
BigMan wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

P: n/a
Could you please provide us with some code to demonstrate your idea?

Jul 23 '05 #6

P: n/a
BigMan wrote:
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::obj(4);
or
class SomeClass
{
static SomeClass *p;

// The rest of your code

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

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

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7

P: n/a
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.

Jul 23 '05 #8

P: n/a
BigMan wrote:
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);

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #9

P: n/a

BigMan wrote:
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

Jul 23 '05 #10

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

Jul 23 '05 #11

P: n/a
BigMan wrote:
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?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.