2011-04-19 62 views
0

如何從這樣的功能接收map<string, factory<BaseClass, ConstructorType> >如何接收地圖<A, B>&從函數/類?

所以我

template <class BaseClass, class ConstructorType> 
map<string, factory<BaseClass, ConstructorType> > get_factories (shared_library & lib) { 
    type_map lib_types; 
    if (!lib.call(lib_types)) { 
     cerr << "Types map not found!" << endl; 

    } 

    map<string, factory<BaseClass, ConstructorType> >& lib_factories(lib_types.get()); 
    if (lib_factories.empty()) { 
     cerr << "Producers not found!" << endl; 

    } 
    return lib_factories; 

} 

,我嘗試的東西,如獲得其值:

map<string, factory<PublicProducerPrototype, int> > producer_factories(); 
producer_factories = get_factories<PublicProducerPrototype, int>(simple_producer); 

我試着概括/簡化一些boost.extension方法爲自己。

那麼如何正確接收map<A, B>&

如何正確初始化鏈接或如何返回不鏈接,但實際的對象? (對不起,C++ NUBE)

回答

1

如果你需要一個地圖的引用,你應該聲明函數爲返回一個參考,而不是一個值:

template <class BaseClass, class ConstructorType> 
map<string, factory<BaseClass, ConstructorType> >& get_factories (... 

當然,這是假定,參考由lib_types.get()返回作爲參考是安全的。