为什么我无法从函数返回 Boost::Scoped_ptr?

发布于 2024-11-02 12:40:39 字数 1623 浏览 4 评论 0原文

所以我尝试围绕 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2024-11-09 12:40:39

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 a scoped_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 supports std::unique_ptr); otherwise, consider using std::auto_ptr or {std,std::tr1,boost}::shared_ptr, depending on what your exact use case is.

苦行僧 2024-11-09 12:40:39

您还可以尝试 boost::interprocess::unique_ptr。

You can also try boost::interprocess::unique_ptr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文