I am currently learning the basics of the STL and boost libraries and wanted some assistance. Let me first describe where I am at I want to construct a vector of shared_ptrs of say some class foo. Previously I had an array or pointers and was required to use new
and delete
to handle memory and thought the transition to vectors and smart pointers would be a good idea. I currently have something like:
class foo{
....
}
int main(){
std::vector< boost::shared_ptr< foo > > vec_ptr;
vec_ptr.push_back(boost::make_shared<foo>);
....
}
Now I would like to know how to best pass this vector in two situations: - Into a function. - Into the constructor of a class object (which via the initialisation list initialises the object.
Into a function I am guessing that it is best passed by reference, as in:
void func(std::vector< boost::shared_ptr < foo > >& vec_ptr_in_func){
....
}
Into a class constructor I am not sure my initial guess was something like
class vec_class{
vec_class(std::vector< boost::shared_ptr < foo > >& vec_ptr_in_class_)
: vec_ptr_in_class(vec_ptr_in_class_){...}
}
But this seems to be giving errors along the lines of:
no matching function for call to vec_class::vec_class(std::vector< boost::shared_ptr < foo >, std::allocator<boost::shared_ptr< foo > > >)
For a function pass it like
// 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){ }
Pass it by const-reference for the constructor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.