简体   繁体   English

将智能指针放置在STL容器中

[英]Placing smart pointers in an STL container

I have a container which I'd like to fill with pointers to class C. However, I'd rather use Boost shared_ptr than dumb pointers. 我有一个容器,我想填充指向C类的指针。但是,我宁愿使用Boost shared_ptr而不是哑指针。

I declare the container as container<C*> , but then get this error: 我将容器声明为container<C*> ,但出现此错误:

  no known conversion for argument 1 from ‘boost::shared_ptr<C>’ to ‘C* const&’

How should I declare the container to take boost shared ptrs? 我应该如何声明该容器使用增强共享ptrs? If possible, I'd like it to take dumb pointers as well. 如果可能的话,我也希望它使用哑指针。


UPDATE: From the responses, it seems that the container must indeed be declared to take smart pointers, it can't be made to take both smart or dumb pointers. 更新:从响应来看,似乎确实必须声明容器采用智能指针,而不能使容器同时采用智能或哑指针。 That is, there's no coercion from smart to dumb or versa. 也就是说,从聪明到愚蠢,反之亦然。 Is that correct? 那是对的吗?

Here is a simple working demo using C++11 shared pointers. 这是一个使用C ++ 11共享指针的简单工作演示。 They are analogous to Boost shared pointers. 它们类似于Boost共享指针。

#include <iostream>
#include <memory>
#include <vector>

int main( int argc, char* argv[] )
{
    // Create vector
    std::vector<std::shared_ptr<int>> buffer;

    for( int i=0; i<10; i++ )
        buffer.push_back(std::make_shared<int>(i));

    for( int i=0; i<10; i++ )
        if( (*buffer[i]) != i ){
            std::cout << "Match invalid for " << i << "." << std::endl;
            return 1;
        }
    std::cout << "Valid result" << std::endl;

    return 0;
}

I compiled this using 我用这个编译了

g++ main.cpp -o main -std=c++11

In order to use vectors you need to explicitly specify the type of objects that they will be holding.In your case it will be boost::shared_ptr . 为了使用向量,您需要明确指定它们将要持有的对象的类型,在您的情况下将为boost::shared_ptr I also understand that you want to store dumb pointers in that container. 我也了解您要在该容器中存储哑指针。 You probably mean raw pointers.As mentioned earlier your container can primarily store one type however there are exceptions for instance the types are related via inheritance or another mechanism (such as serialization) for which some explicit down-casting would be required when you attempt to use those objects or the type is a generic type. 如前所述,您的容器可以主要存储一种类型,但是有一些例外情况,例如,这些类型是通过继承或另一种机制(例如序列化)关联的,当您尝试使用这种机制时需要明确的向下转换使用那些对象,或者类型是泛型类型。 Never the less. 永远不会少。 Here is another approach. 这是另一种方法。 You don't need a vector that stores both a smart pointer and a raw pointer since you could always obtain a raw/dumb pointer from a smart pointer. 您不需要同时存储智能指针和原始指针的向量,因为您始终可以从智能指针获取原始/哑指针。 Your best approach is to create a vector like this 最好的方法是创建这样的向量

std::vector<boost::shared_ptr<foo>> vec;

The above creates a vector that will store shared pointers to foo. 上面创建了一个向量,该向量将存储指向foo的共享指针。

Then when ever you have a shared pointer as such 然后,只要有这样的共享指针

 boost::shared_ptr<foo> foo_ptr(new foo());

You can do something like this 你可以做这样的事情

vec.push_back(foo_ptr)

When you need the dumb pointer you could do this 当您需要哑指针时,可以执行此操作

foo* f = vec[0].get();

I read your update in which you stated 我阅读了您所说的更新

... it seems that the container must indeed be declared to take smart pointers, it can't be made to take either smart or dumb pointers. ……似乎确实必须声明容器采用智能指针,而不能使它采用智能指针或哑指针。

You should understand that boost::shared_ptr<Type> is a smart pointer. 您应该了解boost::shared_ptr<Type>是一个智能指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM