2012-12-12 78 views
0

目前我正在学习STL的基本知识和Boost库,并希望一些帮助。首先,我要说明我在哪里,在我要建构说,一些类Foo的shared_ptrs的载体。以前我有一个数组或指针,并需要使用newdelete来处理内存和思想的载体和智能指针的过渡将是一个不错的主意。目前,我有这样的:C++传递的std ::矢量<提高:: shared_ptr的< foo >>

class foo{ 
.... 
} 

int main(){ 
std::vector< boost::shared_ptr<foo> > vec_ptr; 
vec_ptr.push_back(boost::make_shared<foo>); 
.... 
} 

现在我想知道如何以最佳方式传递这种载体在两种情况下: - 成一个函数。 。 - 成类对象(通过初始化列表初始化对象的构造

进入我猜测,这是最好的通过引用传递功能,如:

void func(std::vector< boost::shared_ptr <foo> >& vec_ptr_in_func){ 
    .... 
} 

成一个类构造函数我不知道我的初步猜测是类似

class vec_class{ 
vec_class(std::vector< boost::shared_ptr <foo> >& vec_ptr_in_class_) 
    : vec_ptr_in_class(vec_ptr_in_class_){...} 
} 

但这似乎沿着线是给错误:

no matching function for call to vec_class::vec_class(std::vector< boost::shared_ptr <foo>, std::allocator<boost::shared_ptr<foo> > >) 
+1

的功能是什么,什么是构造函数,要与载体呢?很大程度上取决于这一点。 – jogojapan

+0

我想你想你的构造公开。默认情况下它是私人的。 –

回答

0
  1. 对于函数通过它像

    // If you intend to modify the vec_ptr_in_func 
    void func(std::vector< boost::shared_ptr <foo> >& vec_ptr_in_func){ 
    
    } 
    
    
    // If you intend to not modify the vec_ptr_in_func 
    void func(const std::vector< boost::shared_ptr <foo> >& vec_ptr_in_func){ 
    
    } 
    
  2. 通行证它由用于构造常量参考。

相关问题