C++ 根据对象名字创建对象

base.hpp的代码: 

#ifndef _H_DYNAMIC_H
#define _H_DYNAMIC_H

#include  " Base.h "
#include <iostream>
#include <map>
using  namespace std;

template<typename T> Base * createT() {  return  new T; }

struct BaseFactory {
    typedef std::map<std:: string, Base*(*)()> map_type;

     static Base * createInstance(std:: string  const& s) {
        map_type::iterator it = getMap()->find(s);
         if(it == getMap()->end())
             return  0;
         return it->second();
    }

     protected:
     static map_type * getMap() {
         if(!map) { map =  new map_type; } 
         return map; 
    }

     public:
     static map_type * map;
};

template<typename T>
struct DerivedRegister : BaseFactory { 
    DerivedRegister(std:: string  const& s) { 
        getMap()->insert(std::make_pair(s, &createT<T>));
        cout<< " add "<<endl;
    }
};
#define REGISTER_DEC_TYPE(NAME) static DerivedRegister<NAME> reg
#define REGISTER_DEF_TYPE(NAME) DerivedRegister<NAME> NAME::reg(#NAME)

#endif  

Base.h的代码:

#ifndef _H_BASE_H
#define _H_BASE_H
#include <iostream>
using  namespace std;

class Base {
     string name;
public:
     virtual  void print();
     virtual ~Base();
};

#endif

Base.cpp的代码: 

#include  " Base.h "

Base::~Base(){
    cout<< " detructed "<<endl;
}

void Base::print(){
    cout<< " base "<<endl;

} 

derivedb.h的代码:

#include  " derivedb.hpp "
DerivedRegister<DerivedB> DerivedB::reg( " DerivedB ");

void DerivedB::print(){
    cout<< " derived "<<endl;

} 

derivedb.hpp的代码:

#ifndef _H_DERIVEDB_H
#define _H_DERIVEDB_H

#include <iostream>
#include  " base.hpp "
#include  " Base.h "
using  namespace std;

class DerivedB :  public Base{
private:
     static DerivedRegister<DerivedB> reg;
public :
     void print();
};

#endif

main的测试代码:

#include  " Animal.h "
#include  " Cat.h "
#include <iostream>
#include  " base.hpp "
#include <dlfcn.h>
#include  " derivedb.hpp "
#include <map>
using  namespace std;

BaseFactory::map_type * BaseFactory::map;
class Base;

void load(){
     void *handle = dlopen( " ./deriveb.so ",RTLD_NOW|RTLD_GLOBAL);
     if(!handle)
    {
        std::cerr << dlerror() << std::endl;
         return;
    }
     else{
        cout<< " load OK "<<endl;
    }

    Base * bp = BaseFactory::createInstance( " DerivedB ");
    bp->print();
}

using  namespace std;
int main( int argc,  char ** argv ){
    load();
    return  0;

} 

编译:

g++ -fPIC -shared -o deriveb.so  derivedb.cpp  

g++ -c Base.cpp 

g++ -rdynamic -g -ldl  main.cpp Base.o

这里有一个非常关键的地方,就是这个-rdynamic,如果不加这个选项,dlopen会包undifined symbol,参考:http://stackoverflow.com/questions/480617/dlopen-issue


 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/welkinwalker/archive/2011/12/06/2278394.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值