为什么我无法从函数返回 Boost::Scoped_ptr?
所以我尝试围绕 boost.extension 函数创建一些包装器来创建类。所以我创建了一个函数:
template <class BaseClass, class ConstructorType>
boost::scoped_ptr<BaseClass> get_class (shared_library & lib, std::string class_name, ConstructorType value ) {
map<string, factory<BaseClass, ConstructorType> > lib_factories = get_factories<BaseClass, ConstructorType>(lib);
return boost::scoped_ptr<BaseClass> lib_class(lib_factories[class_name].create(value));
}
它调用 :
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;
cin.get();
}
map<string, factory<BaseClass, ConstructorType> > lib_factories(lib_types.get());
if (lib_factories.empty()) {
cerr << "Producers not found!" << endl;
cin.get();
}
return lib_factories;
}
但最后一个并不那么重要。重要的是 - 我无法得到我的函数 return=(
我尝试了这样的方式:
boost::scoped_ptr<PublicProducerPrototype> producer = get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1);
我也尝试过:
boost::scoped_ptr<PublicProducerPrototype> producer ( get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1));
但编译器告诉我 C2248
它无法调用 boost::scoped_ptr< 的某些私有成员;T>
那么如何让我的退货...可退货 // 如何接收它?
So I try to create some wrapper around boost.extension functions for class creation. So I have created a function:
template <class BaseClass, class ConstructorType>
boost::scoped_ptr<BaseClass> get_class (shared_library & lib, std::string class_name, ConstructorType value ) {
map<string, factory<BaseClass, ConstructorType> > lib_factories = get_factories<BaseClass, ConstructorType>(lib);
return boost::scoped_ptr<BaseClass> lib_class(lib_factories[class_name].create(value));
}
which calls :
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;
cin.get();
}
map<string, factory<BaseClass, ConstructorType> > lib_factories(lib_types.get());
if (lib_factories.empty()) {
cerr << "Producers not found!" << endl;
cin.get();
}
return lib_factories;
}
but last is not so important. What is important - I can not get my function return=(
I try such way :
boost::scoped_ptr<PublicProducerPrototype> producer = get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1);
I also have tried :
boost::scoped_ptr<PublicProducerPrototype> producer ( get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1));
but compiler talls me C2248
it can not call some private member of boost::scoped_ptr<T>
So how to make my return... returnable // how to recieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::scoped_ptr
是不可复制的。由于您无法复制scoped_ptr
,因此您也无法返回一个对象(按值返回对象要求您能够复制它,至少在 C++03 中是这样)。如果需要返回智能指针拥有的对象,则需要选择不同类型的智能指针。如果您的编译器支持
std::unique_ptr
,您应该使用它(因为看起来您使用的是 Visual C++,所以 Visual C++ 2010 支持std::unique_ptr
);否则,请考虑使用std::auto_ptr
或{std,std::tr1,boost}::shared_ptr
,具体取决于您的具体用例。boost::scoped_ptr
is noncopyable. Since you can't copy ascoped_ptr
, you can't return one either (returning an object by value requires that you are able to make a copy of it, at least in C++03). If you need to return an object owned by a smart pointer, you need to pick a different type of smart pointer.If your compiler supports
std::unique_ptr
you should use that instead (Since it looks like you're using Visual C++, Visual C++ 2010 supportsstd::unique_ptr
); otherwise, consider usingstd::auto_ptr
or{std,std::tr1,boost}::shared_ptr
, depending on what your exact use case is.您还可以尝试 boost::interprocess::unique_ptr。
You can also try boost::interprocess::unique_ptr.