|
发表于 2009-6-22 17:11:00
|
显示全部楼层
Re: 问大家一个不大不小的问题,如何实例化抽象类?
namespace U
{
typedef enum
{
E_IO = 0,
E_NET,
E_UNKNOWN,
}EType;
class IPrint
{
public:
static IPrint* Create(EType eType);
virtual void Do() = 0;
};
class CIOPrint : public IPrint
{
public:
virtual void Do(){}
};
class CNETPrint : public IPrint
{
public:
virtual void Do(){}
};
IPrint* IPrint::Create(EType eType)
{
switch (eType)
{
case E_IO:
return new CIOPrint;
case E_NET:
return new CNETPrint;
default:
break;
}
return NULL;
};
};
using namespace U;
int main()
{
IPrint* obj = IPrint::Create(E_IO);
if (NULL != obj) obj->Do();
return 0;
}
|
|