5,530
社区成员
发帖
与我相关
我的任务
分享
class Base
{
public:
virtual Base* create(){return NULL;};
virtual void Parse(){};
virtual void Destory(){};
Base(){};
~Base(){};
};
class Derive :public Base
{
public:
Base* create()
{
return new Derive;
}
void Parse()
{
cout<<"From Derive\n";
}
void Destory()
{
cout<<"From Derive Destory\n";
}
};
int main()
{
Derive soft;//==============>问题在这:别人使用的时候还是得把这里修改成我写的Derive类
Base* prqt = soft.create();
prqt->Parse();
prqt->Destory();
return 0;
}
//interface.h
class device
{
public:
// ..
//int f1();
//int f2(); ...
};
dll_export device* create_device(const char*);
class Derive1;
class Derive2;
class Base
{
public:
bool create(const int type);
void fun()
{
pBase->funA();
}
virtual void funA()
{
cout << "base::funA" << endl;
}
Base()
{
pBase = NULL;
}
~Base()
{
if(pBase != NULL)
{
delete pBase;
pBase = NULL;
}
};
private:
Base* pBase;
};
class Derive1 :public Base
{
public:
Derive1(){}
void funA(){cout << "Derive1::funA" << endl;};
};
class Derive2 :public Base
{
public:
Derive2(){}
void funA(){cout << "Derive2::funA" << endl;};
};
bool Base::create(const int type)
{
switch(type)
{
case 1:
pBase = new Derive1();
break;
case 2:
pBase = new Derive2();
break;
default:
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
Base b;
b.create(2);
b.fun();
getchar();
return 0;
}