5,529
社区成员
发帖
与我相关
我的任务
分享CService* CService::CreateInstance()
{
if (NULL == m_pInstance)
{
m_pInstance = new CService;
// 既然m_pInstance非NULL return m_pInstance;
// m_pInstance NULL 也是return m_pInstance; (NULL)
// 那下面这个if...else...语句直接用 return m_pInstance;就行了
if (NULL != m_pInstance)
{
return m_pInstance;
}
else
{
return NULL;
}
}
// 其实这里应该return m_pInstance的,因为如果 NULL != m_pInstance该怎么办呢
}
CService* CService::Instance()
{
if (NULL == m_pInstance)
{
const char* servicename=getenv("serviceName");
if(strcmp(servicename,"service1")==0)
m_pInstance = new CService1;
else if(strcmp(servicename,"service2")==0)
m_pInstance = new CService2;
//... 下省略
else
m_pInstance = new CServicen;
}
return m_pInstance;
}
if (NULL != m_pInstance)
{
return m_pInstance;
}
else
{
return NULL;
}