需要特殊类型的单身人士 [英] Special type of singleton needed

查看:79
本文介绍了需要特殊类型的单身人士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设计一个满足以下要求的课程:


-课程可能没有更多的实例;

-实例需要初始化的参数;

- 访问实例不得要求任何参数;


欢迎提出任何想法和意见。

解决方案



BigMan写道:

我需要设计一个满足以下要求的类:

- 该类可能没有更多的实例;
- 实例需要初始化参数;
- 访问实例不得要求任何参数;




您打算如何提供参数?为您的Singleton

类提供静态setArguments(...)方法当然很容易

,在调用规范之前必须调用它。

静态Singleton& getInstance()。

私有ctor / public static getInstance模式保证你的第一个和第三个条件是
,但是没有办法强制执行/>
在编译时调用setArguments。最好你可以从getInstance()抛出



HTH,

Michiel Salters


BigMan写道:

我需要设计一个满足以下要求的课程:

- 课程可能没有更多一个实例;
- 实例需要初始化参数;
- 访问实例不得要求任何参数;

欢迎提出任何想法和意见。




class myClass

{


private:

static myClass * _instance;

int pID;

protected:

public:

myClass(int processID):pid(processID){

}

静态myClass *实例(int processID){

try {

_instance = new myClass(processID)

返回_instance;

} catch(...)

{

}

}


静态myClass *实例(){

ret urn _instance;

}

};


int main()

{

myClass * OBJ = myClass :: Instance(10003);

//

//

//

}


希望这是你想要的。


BigMan写道:< blockquote class =post_quotes>我需要设计一个满足以下要求的类:

- 类可能没有多个实例;
- 实例需要初始化参数;
- 访问实例不得要求任何参数;

欢迎任何想法和意见。




class myClass

{


私人:

静态myClass * _instance;

int pID;

protected:

public:

myClass(int processID):pid(processID){

}

static myClass * Instance(int processID){

try {

_instance = new myClass(processID)

返回_instance;

} catch(...)

{

}

}


静态myClass *实例(){

返回_instance;

}

};


int main()

{

myClass * OBJ = myClass :: Instance(10003);

//

//

//

}


希望这就是你要找的东西。


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.

解决方案


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


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.


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.


这篇关于需要特殊类型的单身人士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆