Example for class factory
- Author
- woelk
- Date
- 02/2006
#include <Base/Common/BaseFactory.hh>
#include <string>
#include <iostream>
using namespace BIAS;
using namespace std;
class Base
{
public:
Base(const string& name)
: Name_(name) {}
inline std::string GetTag() const { return Name_; }
void Who() { cout << "I am "<<Name_<<endl; }
std::string Name_;
};
class A : public Base
{
public:
A() : Base("a") {};
static Base *Allocator() { return dynamic_cast<Base *>(new A); }
};
class B : public Base
{
public:
B() : Base("b") {};
static Base *Allocator() { return dynamic_cast<Base *>(new B); }
};
class C : public Base
{
public:
C() : Base("c") {};
static Base *Allocator() { return dynamic_cast<Base *>(new C); }
};
int main()
{
#ifdef BIAS_BUILD_SHARED_LIBS
#else
#endif
if (!p) return -1;
p->Who();
delete p; p=NULL;
if (!p) return -1;
p->Who();
delete p; p=NULL;
if (!p) return -1;
p->Who();
delete p; p=NULL;
if (p!=NULL){
p->Who();
return -1;
} else {
cerr << "could not generate class with name \"d\""<<endl;
}
return 0;
}