C++ member template for boost ptr_vector
- by Ivan
Hi there,
I'm trying to write a container class using boost::ptr_vector. Inside the ptr_vector I would like to include different classes. I'm trying to achieve that using static templates, but so far I'm not able to do that. For example, the container class is
class model {
private:
boost::ptr_vector<elem_type> elements;
public:
void insert_element(elem_type *a) {
element_list.push_back(a);
}
};
and what I'm trying to achieve is be able to use different elem_type classes. The code below doesn't satisfy my requirements:
template <typename T>class model {
private:
boost::ptr_vector<T> elements;
public:
void insert_element(T *a) {
element_list.push_back(a);
}
};
because when I initialize the container class I can only use one class as template:
model <elem_type_1> model_thing;
model_thing.insert_element(new elem_type_1)
but not elem_type_2:
model_thing.insert_element(new elem_type_2)//error, of course
It is possible to do something like using templates only on the member?
class model {
private:
template <typename T> boost::ptr_vector<T> elements;
public:
void insert_element(T *a) {
element_list.push_back(a);
}
}; //wrong
So I can call the insert_element on the specific class that I want to insert? Note that I do not want to use virtual members.
Thanks!